コード例 #1
0
        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);
        }
コード例 #2
0
 public void CannotInterceptSealedClass()
 {
     Assert.Throws <TypeLoadException>(
         delegate
     {
         VirtualInterceptor.WrapClass(typeof(SealedClass));
     });
 }
コード例 #3
0
 public void CannotInterceptNonPublicClass()
 {
     Assert.Throws <TypeLoadException>(
         delegate
     {
         VirtualInterceptor.WrapClass(typeof(PrivateClass));
     });
 }
コード例 #4
0
        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()));
        }