static Type InterceptInterface(IBuilderContext context, Type typeToBuild, Type originalType, IEnumerable <KeyValuePair <MethodBase, List <IInterceptionHandler> > > handlers, ConstructorInfo ctor, object[] ctorParams) { // Create a wrapper class which implements the interface typeToBuild = InterfaceInterceptor.WrapInterface(originalType); // Create an instance of the concrete class using the policy data object wrappedObject = ctor.Invoke(ctorParams); // Create the proxy that's used by the wrapper ILEmitProxy proxy = new ILEmitProxy(handlers); // Create a new policy which calls the proper constructor ConstructorInfo newConstructor = typeToBuild.GetConstructor(new Type[] { typeof(ILEmitProxy), originalType }); ConstructorCreationPolicy newPolicy = new ConstructorCreationPolicy(newConstructor, new ValueParameter <ILEmitProxy>(proxy), new ValueParameter(originalType, wrappedObject)); context.Policies.Set <ICreationPolicy>(newPolicy, typeToBuild); // Return the wrapped type for building return(typeToBuild); }
static TInterface WrapAndCreateType <TInterface, TConcrete>(IEnumerable <KeyValuePair <MethodBase, List <IInterceptionHandler> > > handlers) where TConcrete : TInterface { Type wrappedType = InterfaceInterceptor.WrapInterface(typeof(TInterface)); ILEmitProxy proxy = new ILEmitProxy(handlers); object target = Activator.CreateInstance(typeof(TConcrete)); List <object> wrappedCtorArgs = new List <object>(); wrappedCtorArgs.Add(proxy); wrappedCtorArgs.Add(target); return((TInterface)Activator.CreateInstance(wrappedType, wrappedCtorArgs.ToArray())); }