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); }
protected override void AddParametersToPolicy(IBuilderContext context, object buildKey, IMemberInfo <ConstructorInfo> member, IEnumerable <IParameter> parameters) { ConstructorCreationPolicy policy = new ConstructorCreationPolicy(member.MemberInfo, parameters); context.Policies.Set <ICreationPolicy>(policy, buildKey); }
public void NullContext() { ConstructorInfo constructor = typeof(Dummy).GetConstructor(new Type[] { typeof(int) }); ConstructorCreationPolicy policy = new ConstructorCreationPolicy(constructor); Assert.Throws <ArgumentNullException>( delegate { policy.Create(null, typeof(Dummy)); }); }
public void CreatesObjectAndPassesValue() { MockBuilderContext context = new MockBuilderContext(); ConstructorInfo constructor = typeof(Dummy).GetConstructor(new Type[] { typeof(int) }); ConstructorCreationPolicy policy = new ConstructorCreationPolicy(constructor, Params(42)); Dummy result = (Dummy)policy.Create(context, typeof(Dummy)); Assert.NotNull(result); Assert.Equal(42, result.val); }
public void NonMatchingParameterTypes() { MockBuilderContext context = new MockBuilderContext(); ConstructorInfo constructor = typeof(Dummy).GetConstructor(new Type[] { typeof(int) }); ConstructorCreationPolicy policy = new ConstructorCreationPolicy(constructor, Params("foo")); Assert.Throws <ArgumentException>( delegate { policy.Create(context, typeof(Dummy)); }); }