Last active
August 29, 2015 14:24
-
-
Save davidbreyer/f342d769264fdca81696 to your computer and use it in GitHub Desktop.
Example of a Unity Interceptor.
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 class ProfilingInterceptionBehavior : IInterceptionBehavior | |
| { | |
| [Dependency] | |
| public ILog Log { get; set; } | |
| public IMethodReturn Invoke(IMethodInvocation input, | |
| GetNextInterceptionBehaviorDelegate getNext) | |
| { | |
| // Code here will execute prior to the method that was called. | |
| // Add any custom code you want to execute prior to intercepted method here. | |
| // Invoke the next behavior in the chain. | |
| var result = getNext()(input, getNext); | |
| // Code here will execute after the method that was called. | |
| // Add any custom code you want to execute after the intercepted method here. | |
| if (result.Exception != null) | |
| { | |
| WriteLog(String.Format( | |
| "Method {0} threw exception {1}.", | |
| input.MethodBase, result.Exception.Message)); | |
| } | |
| return result; | |
| } | |
| public IEnumerable<Type> GetRequiredInterfaces() | |
| { | |
| return Type.EmptyTypes; | |
| } | |
| public bool WillExecute | |
| { | |
| get { return true; } | |
| } | |
| private void WriteLog(string message) | |
| { | |
| if (Log != null) | |
| { | |
| Log.DebugFormat("Profiler: {0}", message); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment