Example #1
0
        private static void DefineSerializationConstructor(ModuleDefinition module, TypeDefinition targetType)
        {
            MethodReference getTypeFromHandle = module.ImportMethod <Type>("GetTypeFromHandle",
                                                                           BindingFlags.Public | BindingFlags.Static);

            var parameterTypes = new[] { typeof(SerializationInfo), typeof(StreamingContext) };

            // Define the constructor signature
            MethodDefinition serializationCtor = targetType.AddDefaultConstructor();

            serializationCtor.AddParameters(parameterTypes);

            serializationCtor.Attributes = MethodAttributes.HideBySig | MethodAttributes.SpecialName |
                                           MethodAttributes.RTSpecialName | MethodAttributes.Public;
            TypeReference      interceptorInterfaceType = module.ImportType <IInterceptor>();
            VariableDefinition interceptorTypeVariable  = serializationCtor.AddLocal <Type>();

            MethodBody body = serializationCtor.Body;

            body.InitLocals = true;

            CilWorker IL = serializationCtor.GetILGenerator();

            IL.Emit(OpCodes.Ldtoken, interceptorInterfaceType);
            IL.Emit(OpCodes.Call, getTypeFromHandle);
            IL.Emit(OpCodes.Stloc, interceptorTypeVariable);

            MethodReference defaultConstructor = module.ImportConstructor <object>();

            IL.Emit(OpCodes.Ldarg_0);
            IL.Emit(OpCodes.Call, defaultConstructor);

            // __interceptor = (IInterceptor)info.GetValue("__interceptor", typeof(IInterceptor));
            MethodReference getValue = module.ImportMethod <SerializationInfo>("GetValue");

            IL.Emit(OpCodes.Ldarg_0);
            IL.Emit(OpCodes.Ldarg_1);
            IL.Emit(OpCodes.Ldstr, "__interceptor");
            IL.Emit(OpCodes.Ldloc, interceptorTypeVariable);
            IL.Emit(OpCodes.Callvirt, getValue);
            IL.Emit(OpCodes.Castclass, interceptorInterfaceType);

            MethodReference setInterceptor = module.ImportMethod <IProxy>("set_Interceptor");

            IL.Emit(OpCodes.Callvirt, setInterceptor);
            ;
            IL.Emit(OpCodes.Ret);
        }
        /// <summary>
        /// Adds a new method to the <paramref name="typeDef">target type</paramref>.
        /// </summary>
        /// <param name="typeDef">The type that will hold the newly-created method.</param>
        /// <param name="attributes">The <see cref="Mono.Cecil.MethodAttributes"/> parameter that describes the characteristics of the method.</param>
        /// <param name="methodName">The name to be given to the new method.</param>
        /// <param name="returnType">The method return type.</param>
        /// <param name="callingConvention">The calling convention of the method being created.</param>
        /// <param name="parameterTypes">The list of argument types that will be used to define the method signature.</param>
        /// <returns>A <see cref="MethodDefinition"/> instance that represents the newly-created method.</returns>
        public static MethodDefinition DefineMethod(this TypeDefinition typeDef, string methodName,
                                                    MethodAttributes attributes, MethodCallingConvention callingConvention, Type returnType, params Type[] parameterTypes)
        {
            var method = new MethodDefinition(methodName, attributes, null)
            {
                CallingConvention = callingConvention
            };

            typeDef.Methods.Add(method);

            // Match the parameter types
            method.AddParameters(parameterTypes);

            // Match the return type
            method.SetReturnType(returnType);

            return(method);
        }
        /// <summary>
        /// Adds a new method to the <paramref name="typeDef">target type</paramref>.
        /// </summary>
        /// <param name="typeDef">The type that will hold the newly-created method.</param>
        /// <param name="attributes">The <see cref="MethodAttributes"/> parameter that describes the characteristics of the method.</param>
        /// <param name="methodName">The name to be given to the new method.</param>
        /// <param name="returnType">The method return type.</param>
        /// <param name="parameterTypes">The list of argument types that will be used to define the method signature.</param>
        /// <param name="genericParameterTypes">The list of generic argument types that will be used to define the method signature.</param>
        /// <returns>A <see cref="MethodDefinition"/> instance that represents the newly-created method.</returns>
        public static MethodDefinition DefineMethod(this TypeDefinition typeDef, string methodName,
                                                    MethodAttributes attributes, Type returnType, Type[] parameterTypes, Type[] genericParameterTypes)
        {
            var method = new MethodDefinition(methodName, attributes, null);

            typeDef.Methods.Add(method);

            //Match the generic parameter types
            foreach (var genericParameterType in genericParameterTypes)
            {
                method.AddGenericParameter(genericParameterType);
            }

            // Match the parameter types
            method.AddParameters(parameterTypes);

            // Match the return type
            method.SetReturnType(returnType);

            return(method);
        }
        /// <summary>
        /// Adds a new method to the <paramref name="typeDef">target type</paramref>.
        /// </summary>
        /// <param name="typeDef">The type that will hold the newly-created method.</param>
        /// <param name="attributes">The <see cref="Mono.Cecil.MethodAttributes"/> parameter that describes the characteristics of the method.</param>
        /// <param name="methodName">The name to be given to the new method.</param>
        /// <param name="returnType">The method return type.</param>
        /// <param name="parameterTypes">The list of argument types that will be used to define the method signature.</param>
        /// <param name="genericParameterTypes">The list of generic argument types that will be used to define the method signature.</param>
        /// <returns>A <see cref="MethodDefinition"/> instance that represents the newly-created method.</returns>
        public static MethodDefinition DefineMethod(this TypeDefinition typeDef, string methodName,
                                                    Mono.Cecil.MethodAttributes attributes, System.Type returnType, System.Type[] parameterTypes, System.Type[] genericParameterTypes)
        {
            var method = new MethodDefinition(methodName, attributes, typeDef.Module.Import(typeof(object)));

            typeDef.Methods.Add(method);

            //Match the generic parameter types
            foreach (var genericParameterType in genericParameterTypes)
            {
                method.AddGenericParameter(genericParameterType);
            }

            // Match the parameter types
            method.AddParameters(parameterTypes);

            // Match the return type
            method.SetReturnType(returnType);

            return(method);
        }