Пример #1
0
        internal static DynamicProxy CreateProxy(this Type type)
        {
            ILGenerator ilGenerator = null;

            ConstructorInfo[]       constructors       = null;
            Type[]                  parameterTypes     = Type.EmptyTypes;
            MappedInvocation        mappedInvocation   = null;
            List <Cil.Instruction>  instructions       = null;
            ConstructorBuilder      constructorBuilder = null;
            List <MappedInvocation> mappedInvocations  = null;

            Cecil.MethodDefinition            baseCtorInvocation    = null;
            Cecil.MethodDefinition            constructorDefinition = null;
            NRefactory.ConstructorDeclaration resolvedCtor          = null;
            string      typeName    = string.Format("{0}.{1}", DynamicAssemblyBuilder.AssemblyName, type.FullName);
            TypeBuilder typeBuilder = DynamicModuleBuilder.Instance.DefineType(typeName, _typeAttributes, type);

            Interlocked.Exchange <TypeBuilder>(ref _current, typeBuilder);

            if (type.IsGenericType)
            {
                MakeGenericType(type, typeBuilder);
            }

            constructors      = type.GetConstructors();
            mappedInvocations = new List <MappedInvocation>(constructors.Length);

            foreach (var constructor in constructors)
            {
                parameterTypes = constructor.GetParameters()
                                 .Select(p => p.ParameterType)
                                 .ToArray();

                constructorDefinition = constructor.ResolveConstructorDefinition();
                resolvedCtor          = constructorDefinition.ResolveMothod <NRefactory.ConstructorDeclaration>();
                instructions          = FilterInstructions(constructorDefinition.Body.Instructions, type.BaseType);
                baseCtorInvocation    = instructions[instructions.Count - 2].Operand as Cecil.MethodDefinition;
                mappedInvocation      = new MappedInvocation(resolvedCtor, baseCtorInvocation);
                mappedInvocations.Add(mappedInvocation);
                constructorBuilder = typeBuilder.DefineConstructor(constructor.Attributes, _callingConventions, mappedInvocation.Parameters);
                ilGenerator        = constructorBuilder.GetILGenerator();
                resolvedCtor       = ResolveConstructor(resolvedCtor, type.BaseType, ilGenerator, instructions);
            }

            return(new DynamicProxy(typeBuilder.CreateType(), type, mappedInvocations));
        }
        internal static DynamicProxy CreateProxy(this Type type) {
            ILGenerator ilGenerator = null;
            ConstructorInfo[] constructors = null;
            Type[] parameterTypes = Type.EmptyTypes;
            MappedInvocation mappedInvocation = null;
            List<Cil.Instruction> instructions = null;
            ConstructorBuilder constructorBuilder = null;
            List<MappedInvocation> mappedInvocations = null;
            Cecil.MethodDefinition baseCtorInvocation = null;
            Cecil.MethodDefinition constructorDefinition = null;
            NRefactory.ConstructorDeclaration resolvedCtor = null;
            string typeName = string.Format("{0}.{1}", DynamicAssemblyBuilder.AssemblyName, type.FullName);
            TypeBuilder typeBuilder = DynamicModuleBuilder.Instance.DefineType(typeName, _typeAttributes, type);

            Interlocked.Exchange<TypeBuilder>(ref _current, typeBuilder);

            if (type.IsGenericType) {
                MakeGenericType(type, typeBuilder);
            }

            constructors = type.GetConstructors();
            mappedInvocations = new List<MappedInvocation>(constructors.Length);

            foreach (var constructor in constructors) {
                parameterTypes = constructor.GetParameters()
                                            .Select(p => p.ParameterType)
                                            .ToArray();

                constructorDefinition = constructor.ResolveConstructorDefinition();
                resolvedCtor = constructorDefinition.ResolveMothod<NRefactory.ConstructorDeclaration>();
                instructions = FilterInstructions(constructorDefinition.Body.Instructions, type.BaseType);
                baseCtorInvocation = instructions[instructions.Count - 2].Operand as Cecil.MethodDefinition;
                mappedInvocation = new MappedInvocation(resolvedCtor, baseCtorInvocation);
                mappedInvocations.Add(mappedInvocation);
                constructorBuilder = typeBuilder.DefineConstructor(constructor.Attributes, _callingConventions, mappedInvocation.Parameters);
                ilGenerator = constructorBuilder.GetILGenerator();
                resolvedCtor = ResolveConstructor(resolvedCtor, type.BaseType, ilGenerator, instructions);
            }

            return new DynamicProxy(typeBuilder.CreateType(), type, mappedInvocations);
        }