Last active
August 29, 2015 14:00
-
-
Save mgroves/11149413 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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