IMethodInterceptor Interface |
Namespace: Cauldron.Interception
The IMethodInterceptor type exposes the following members.
Name | Description | |
---|---|---|
OnEnter |
Invoked if an intercepted method has been called
| |
OnException |
Invoked if an intercepted method has raised an exception.
| |
OnExit |
Invoked if the intercepted method has finished executing.
|
[InterceptorOptions(AlwaysCreateNewInstance = true)] [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)] public class MyInterceptorAttribute : Attribute, IMethodInterceptor { public void OnEnter(Type declaringType, object instance, MethodBase methodbase, object[] values) { } public bool OnException(Exception e) { // Returning false will swallow the exception return true; } public void OnExit() { } }
public class SampleClass { [MyInterceptor] public void SampleMethod() { Debug.WriteLine("Blablablablablabla"); } }
public class SampleClass { public void SampleMethod() { var interceptorAttribute = new MyInterceptorAttribute(); try { interceptorAttribute.OnEnter(typeof(SampleClass), this, MethodBase.GetMethodFromHandle(methodof(SampleClass.SampleMethod()).MethodHandle, typeof(SampleClass).TypeHandle), new object[0]); Debug.WriteLine("Blablablablablabla"); } catch (Exception e) { if(interceptorAttribute.OnException(e)) { throw; } } finally { interceptorAttribute.OnExit(); } } }