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 Type InterceptClass(IBuilderContext context, Type typeToBuild, IEnumerable <KeyValuePair <MethodBase, List <IInterceptionHandler> > > handlers, object[] originalParameters) { // Create a wrapper class which derives from the intercepted class typeToBuild = VirtualInterceptor.WrapClass(typeToBuild); // Create the proxy that's used by the wrapper ILEmitProxy proxy = new ILEmitProxy(handlers); // Create a new policy which calls the proper constructor List <Type> newParameterTypes = new List <Type>(); List <IParameter> newIParameters = new List <IParameter>(); newParameterTypes.Add(typeof(ILEmitProxy)); newIParameters.Add(new ValueParameter <ILEmitProxy>(proxy)); foreach (object obj in originalParameters) { newParameterTypes.Add(obj.GetType()); newIParameters.Add(new ValueParameter(obj.GetType(), obj)); } ConstructorInfo newConstructor = typeToBuild.GetConstructor(newParameterTypes.ToArray()); ConstructorCreationPolicy newPolicy = new ConstructorCreationPolicy(newConstructor, newIParameters.ToArray()); context.Policies.Set <ICreationPolicy>(newPolicy, typeToBuild); // Return the wrapped type for building return(typeToBuild); }
static T WrapAndCreateType <T>(IEnumerable <KeyValuePair <MethodBase, List <IInterceptionHandler> > > handlers, params object[] ctorArgs) { Type wrappedType = VirtualInterceptor.WrapClass(typeof(T)); ILEmitProxy proxy = new ILEmitProxy(handlers); List <object> wrappedCtorArgs = new List <object>(); wrappedCtorArgs.Add(proxy); wrappedCtorArgs.AddRange(ctorArgs); return((T)Activator.CreateInstance(wrappedType, wrappedCtorArgs.ToArray())); }
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())); }