コード例 #1
0
        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);
        }
コード例 #2
0
        public override MethodBase GetMethodBaseForPolicy(Type typeRequested,
                                                          MethodBase method)
        {
            if (!typeRequested.IsInterface)
            {
                return(null);
            }

            ParameterInfo[] paramInfos = method.GetParameters();
            object[]        paramTypes = new object[paramInfos.Length];

            for (int idx = 0; idx < paramInfos.Length; ++idx)
            {
                if (paramInfos[idx].ParameterType.IsGenericParameter)
                {
                    paramTypes[idx] = paramInfos[idx].ParameterType.Name;
                }
                else
                {
                    paramTypes[idx] = paramInfos[idx].ParameterType;
                }
            }

            return(InterfaceInterceptor.FindMethod(method.Name, paramTypes, typeRequested.GetMethods()));
        }
コード例 #3
0
            public void Overloads()
            {
                MethodInfo result1 = InterfaceInterceptor.FindMethod("Overload", new Type[0], typeof(IFoo <>).GetMethods());
                MethodInfo result2 = InterfaceInterceptor.FindMethod("Overload", new Type[] { typeof(int) }, typeof(IFoo <>).GetMethods());
                MethodInfo result3 = InterfaceInterceptor.FindMethod("Overload", new Type[] { typeof(double) }, typeof(IFoo <>).GetMethods());

                Assert.NotNull(result1);
                Assert.NotNull(result2);
                Assert.Null(result3);
                Assert.NotSame(result1, result2);
            }
コード例 #4
0
        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()));
        }
コード例 #5
0
            public void ParameterTypesNotMatching()
            {
                MethodInfo result = InterfaceInterceptor.FindMethod("ToString", new Type[] { typeof(int) }, typeof(Object).GetMethods());

                Assert.Null(result);
            }
コード例 #6
0
            public void NonGenericClass()
            {
                MethodInfo result = InterfaceInterceptor.FindMethod("ToString", new Type[0], typeof(Object).GetMethods());

                Assert.Same(typeof(Object).GetMethod("ToString"), result);
            }
コード例 #7
0
            public void MethodNotFound()
            {
                MethodInfo result = InterfaceInterceptor.FindMethod("ThisMethodDoesNotExist", new Type[0], typeof(Object).GetMethods());

                Assert.Null(result);
            }
コード例 #8
0
            public void GenericClassNonGenericMethod()
            {
                MethodInfo result = InterfaceInterceptor.FindMethod("RemoveAt", new Type[] { typeof(int) }, typeof(IList <>).GetMethods());

                Assert.Same(typeof(IList <>).GetMethod("RemoveAt"), result);
            }
コード例 #9
0
            public void GenericClassGenericMethodWithExtraGenerics()
            {
                MethodInfo result = InterfaceInterceptor.FindMethod("Bar", new object[] { "T", "T2" }, typeof(IFoo <>).GetMethods());

                Assert.Same(typeof(IFoo <>).GetMethod("Bar"), result);
            }
コード例 #10
0
            public void GenericClassGenericMethod()
            {
                MethodInfo result = InterfaceInterceptor.FindMethod("Insert", new object[] { typeof(int), "T" }, typeof(IList <>).GetMethods());

                Assert.Same(typeof(IList <>).GetMethod("Insert"), result);
            }