Skip to content

Instantly share code, notes, and snippets.

@mgroves
Last active August 29, 2015 14:00
Show Gist options
  • Select an option

  • Save mgroves/11149413 to your computer and use it in GitHub Desktop.

Select an option

Save mgroves/11149413 to your computer and use it in GitHub Desktop.
public void OnInvoke(MethodInterceptionArgs args)
{
try
{
args.Proceed();
}
catch (Exception ex)
{
// ... snip ...
ErrorReturn(args);
}
}
void ErrorReturn(MethodInterceptionArgs args)
{
try
{
var methodInfo = args.Method as MethodInfo;
if (methodInfo == null) // cast failed, so just give up
return;
else if (typeof(IEnumerable).IsAssignableFrom(methodInfo.ReturnType))
args.ReturnValue = Activator.CreateInstance(methodInfo.ReturnType);
}
catch
{
// don't want any errors to take place trying to build the correct return value
// so just swallow them, and let args.ReturnValue stay whatever it is
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment