Skip to content

Instantly share code, notes, and snippets.

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

  • Save davidbreyer/f342d769264fdca81696 to your computer and use it in GitHub Desktop.

Select an option

Save davidbreyer/f342d769264fdca81696 to your computer and use it in GitHub Desktop.
Example of a Unity Interceptor.
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