IConstructorInterceptor Interface |
Namespace: Cauldron.Interception
public interface IConstructorInterceptor
The IConstructorInterceptor type exposes the following members.
Name | Description | |
---|---|---|
OnBeforeInitialization |
Invoked before the initialization of the class.
This happens before the base call which means that the instance has not been created yet. Use this with care.
| |
OnEnter |
Invoked if an intercepted contructor has been called.
| |
OnException |
Invoked if an intercepted construtor has raised an exception. The constructor will always rethrow
the exception.
| |
OnExit |
Invoked if the intercepted constructor has finished executing.
|
[AttributeUsage(AttributeTargets.Constructor, AllowMultiple = false, Inherited = false)] public sealed class TestConstructorInterceptorA : Attribute, IConstructorInterceptor { public void OnBeforeInitialization(Type declaringType, MethodBase methodbase, object[] values) { } 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 ConstructorInterceptorTestClass { [TestConstructorInterceptorA] public ConstructorInterceptorTestClass(string arg) { } }
public ConstructorInterceptorTestClass(string arg) { var values = new object[] { arg }; var constructorInterceptor = new TestConstructorInterceptorA(); constructorInterceptor.OnBeforeInitialization(typeof(ConstructorInterceptorTestClass), MethodBase.GetMethodFromHandle(methodof(ConstructorInterceptorTestClass..ctor()).MethodHandle, typeof(ConstructorInterceptorTestClass).TypeHandle), values); base..ctor(); try { constructorInterceptor.OnEnter(typeof(ConstructorInterceptorTestClass), this, MethodBase.GetMethodFromHandle(methodof(ConstructorInterceptorTestClass..ctor()).MethodHandle, typeof(ConstructorInterceptorTestClass).TypeHandle), values); } catch (Exception e) { if(constructorInterceptor.OnException(e)) { throw; } } finally { constructorInterceptor.OnExit(); } }