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);
        }
 public void CannotInterceptSealedClass()
 {
     Assert.Throws <TypeLoadException>(
         delegate
     {
         VirtualInterceptor.WrapClass(typeof(SealedClass));
     });
 }
 public void CannotInterceptNonPublicClass()
 {
     Assert.Throws <TypeLoadException>(
         delegate
     {
         VirtualInterceptor.WrapClass(typeof(PrivateClass));
     });
 }
        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()));
        }