public static void Build(CustomAttribute customAttribute)
 {
     customAttribute.Constructor =
         new MethodReference(
             ".ctor",
             new TypeReference(
                 "SerializableAttribute",
                 "System",
                 AssemblyReference.GetMscorlib(customAttribute.Assembly)),
             new CallSite(
                 true,
                 false,
                 MethodCallingConvention.Default,
                 TypeReference.GetPrimitiveType(PrimitiveTypeCode.Void, customAttribute.Assembly),
                 ReadOnlyList <TypeSignature> .Empty,
                 0,
                 0));
 }
Esempio n. 2
0
        public override void Build(CustomAttribute customAttribute)
        {
            customAttribute.Constructor =
                new MethodReference(
                    ".ctor",
                    new TypeReference(
                        "ObfuscateAssemblyAttribute",
                        "System.Reflection",
                        AssemblyReference.GetMscorlib(customAttribute.Assembly)),
                    new CallSite(
                        true,
                        false,
                        MethodCallingConvention.Default,
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.Void, customAttribute.Assembly),
                        new TypeSignature[]
            {
                TypeReference.GetPrimitiveType(PrimitiveTypeCode.Boolean, customAttribute.Assembly),
            },
                        0,
                        0));

            // Ctor arguments
            var ctorArguments = customAttribute.CtorArguments;

            ctorArguments.Clear();
            ctorArguments.Add(
                new CustomAttributeTypedArgument(
                    _assemblyIsPrivate,
                    TypeReference.GetPrimitiveType(PrimitiveTypeCode.Boolean, customAttribute.Assembly)));

            // Named arguments
            var namedArguments = customAttribute.NamedArguments;

            namedArguments.Clear();

            // StripAfterObfuscation
            namedArguments.Add(
                new CustomAttributeNamedArgument(
                    "StripAfterObfuscation",
                    CustomAttributeNamedArgumentType.Property,
                    new CustomAttributeTypedArgument(
                        _stripAfterObfuscation,
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.Boolean, customAttribute.Assembly))));
        }
        private void LoadActions()
        {
            var mscorlib = AssemblyReference.GetMscorlib(_module.Assembly);

            for (int i = 0; i < 9; i++)
            {
                var delegateType = new DelegateType();

                var arguments = new TypeSignature[i];
                for (int j = 0; j < i; j++)
                {
                    arguments[j] = new GenericParameterType(false, j);
                }

                delegateType.InvokeCallSite =
                    new CallSite(
                        true,
                        false,
                        MethodCallingConvention.Default,
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.Void, _module.Assembly),
                        arguments,
                        -1,
                        0);

                string name = "Action";
                if (i > 0)
                {
                    name += "`" + i.ToString();
                }

                delegateType.DeclaringType = new TypeReference(
                    name,
                    "System",
                    mscorlib,
                    false);

                delegateType.GenericParameterCount = i;

                delegateType.InvokeParameterFlags = new int[i];

                _types.TryAdd(CreateTypeInfo(delegateType));
            }
        }
Esempio n. 4
0
        private void Generate(DelegateType delegateType)
        {
            if (delegateType.DeclaringType.Owner != null)
            {
                return;
            }

            var type = _module.Types.Add();

            var declaringTypeRef = delegateType.DeclaringType;

            type.Name       = declaringTypeRef.Name;
            type.Namespace  = declaringTypeRef.Namespace;
            type.Visibility = TypeVisibilityFlags.Public;
            type.IsSealed   = true;

            type.BaseType =
                new TypeReference(
                    "MulticastDelegate", "System",
                    AssemblyReference.GetMscorlib(_module.Assembly), false);

            // Generic parameters
            if (delegateType.GenericParameterCount > 0)
            {
                var genericParameters = type.GenericParameters;
                for (int i = 0; i < delegateType.GenericParameterCount; i++)
                {
                    genericParameters.Add();
                }
            }

            // Methods
            {
                var methods = type.Methods;
                GenerateCtor(methods.Add());
                GenerateInvoke(methods.Add(), delegateType);
            }
        }
        public AssemblyReference ToReference(Assembly ownerAssembly)
        {
            switch (_knownCode)
            {
            case BamlKnownAssemblyCode.MsCorLib:
                return(AssemblyReference.GetMscorlib(ownerAssembly));

            case BamlKnownAssemblyCode.System:
                return(AssemblyReference.GetSystem(ownerAssembly));

            case BamlKnownAssemblyCode.PresentationCore:
                return(ToReference("PresentationCore", ownerAssembly));

            case BamlKnownAssemblyCode.PresentationFramework:
                return(ToReference("PresentationFramework", ownerAssembly));

            case BamlKnownAssemblyCode.WindowsBase:
                return(ToReference("WindowsBase", ownerAssembly));

            default:
                return(null);
            }
        }
        private void LoadFuncs()
        {
            var mscorlib = AssemblyReference.GetMscorlib(_module.Assembly);

            for (int i = 0; i < 9; i++)
            {
                var delegateType = new DelegateType();

                var arguments = new TypeSignature[i];
                for (int j = 0; j < i; j++)
                {
                    arguments[j] = new GenericParameterType(false, j);
                }

                delegateType.InvokeCallSite =
                    new CallSite(
                        true,
                        false,
                        MethodCallingConvention.Default,
                        new GenericParameterType(false, i),
                        arguments,
                        -1,
                        0);

                delegateType.DeclaringType = new TypeReference(
                    "Func`" + (i + 1).ToString(),
                    "System",
                    mscorlib,
                    false);

                delegateType.GenericParameterCount = i + 1;

                delegateType.InvokeParameterFlags = new int[i];

                _types.TryAdd(CreateTypeInfo(delegateType));
            }
        }
        private void GeneraterFields()
        {
            // MethodPointers : [mscorlib]System.IntPtr[]
            var field = _mainType.Fields.Add();

            field.Name       = "MethodPointers";
            field.Visibility = FieldVisibilityFlags.Assembly;
            field.IsStatic   = true;
            field.FieldType  =
                new ArrayType(
                    TypeReference.GetPrimitiveType(PrimitiveTypeCode.IntPtr, _module.Assembly));

            // _dataStream : System.IO.Stream
            if (null == _mainType.Fields.Find("_dataStream"))
            {
                field            = _mainType.Fields.Add();
                field.Name       = "_dataStream";
                field.Visibility = FieldVisibilityFlags.Assembly;
                field.IsStatic   = true;
                field.FieldType  =
                    new TypeReference("Stream", "System.IO",
                                      AssemblyReference.GetMscorlib(_module.Assembly), false);
            }
        }
Esempio n. 8
0
        /// <summary>
        /// static LoadStrings() : [mscorlib]System.Void
        /// </summary>
        private void LoadStrings(MethodDeclaration method)
        {
            method.Name        = "LoadStrings";
            method.Visibility  = MethodVisibilityFlags.Assembly;
            method.IsStatic    = true;
            method.IsHideBySig = true;

            // Return type
            {
                var returnType = method.ReturnType;
                returnType.Type =
                    TypeReference.GetPrimitiveType(PrimitiveTypeCode.Void, _module.Assembly);
            }

            // Body
            {
                var methodBody = new MethodBody();
                methodBody.MaxStackSize = 5;
                methodBody.InitLocals   = true;

                // Local variables
                {
                    var localVariables = methodBody.LocalVariables;
                    localVariables.Add(
                        new ArrayType(
                            TypeReference.GetPrimitiveType(PrimitiveTypeCode.UInt8, _module.Assembly)));
                    localVariables.Add(
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.Int32, _module.Assembly));
                    localVariables.Add(
                        new TypeReference("Encoding", "System.Text",
                                          AssemblyReference.GetMscorlib(_module.Assembly), false));
                    localVariables.Add(
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.Int32, _module.Assembly));
                }

                // Instructions
                {
                    var instructions = methodBody.Instructions;
                    instructions.Add(new Instruction(OpCodes.Ldc_I4, (int)_dataID));
                    instructions.Add(new Instruction(OpCodes.Call,
                                                     new MethodReference(
                                                         "GetData",
                                                         new TypeReference(_mainType.Name, _mainType.Namespace, false),
                                                         new CallSite(
                                                             false,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             new ArrayType(
                                                                 TypeReference.GetPrimitiveType(PrimitiveTypeCode.UInt8, _module.Assembly)),
                                                             new TypeSignature[]
                    {
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.Int32, _module.Assembly),
                    },
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Stloc_0));
                    instructions.Add(new Instruction(OpCodes.Ldc_I4_0));
                    instructions.Add(new Instruction(OpCodes.Stloc_1));
                    instructions.Add(new Instruction(OpCodes.Call,
                                                     new MethodReference(
                                                         "get_UTF8",
                                                         new TypeReference("Encoding", "System.Text",
                                                                           AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                         new CallSite(
                                                             false,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             new TypeReference("Encoding", "System.Text",
                                                                               AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                             new TypeSignature[0],
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Stloc_2));
                    instructions.Add(new Instruction(OpCodes.Ldc_I4, (int)_strings.Count));
                    instructions.Add(new Instruction(OpCodes.Newarr,
                                                     TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, _module.Assembly)));
                    instructions.Add(new Instruction(OpCodes.Stsfld,
                                                     new FieldReference(
                                                         "Strings",
                                                         new ArrayType(
                                                             TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, _module.Assembly)),
                                                         new TypeReference(_mainType.Name, _mainType.Namespace, false))));
                    instructions.Add(new Instruction(OpCodes.Ldc_I4_0));
                    instructions.Add(new Instruction(OpCodes.Stloc_3));
                    instructions.Add(new Instruction(OpCodes.Br_S, (sbyte)20));
                    instructions.Add(new Instruction(OpCodes.Ldsfld,
                                                     new FieldReference(
                                                         "Strings",
                                                         new ArrayType(
                                                             TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, _module.Assembly)),
                                                         new TypeReference(_mainType.Name, _mainType.Namespace, false))));
                    instructions.Add(new Instruction(OpCodes.Ldloc_3));
                    instructions.Add(new Instruction(OpCodes.Ldloc_0));
                    instructions.Add(new Instruction(OpCodes.Ldloca_S, (byte)1));
                    instructions.Add(new Instruction(OpCodes.Ldloc_2));
                    instructions.Add(new Instruction(OpCodes.Call,
                                                     new MethodReference(
                                                         "ReadStringIntern",
                                                         new TypeReference(_mainType.Name, _mainType.Namespace, false),
                                                         new CallSite(
                                                             false,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, _module.Assembly),
                                                             new TypeSignature[]
                    {
                        new ArrayType(
                            TypeReference.GetPrimitiveType(PrimitiveTypeCode.UInt8, _module.Assembly)),
                        new ByRefType(
                            TypeReference.GetPrimitiveType(PrimitiveTypeCode.Int32, _module.Assembly)),
                        new TypeReference("Encoding", "System.Text",
                                          AssemblyReference.GetMscorlib(_module.Assembly), false),
                    },
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Stelem_Ref));
                    instructions.Add(new Instruction(OpCodes.Ldloc_3));
                    instructions.Add(new Instruction(OpCodes.Ldc_I4_1));
                    instructions.Add(new Instruction(OpCodes.Add));
                    instructions.Add(new Instruction(OpCodes.Stloc_3));
                    instructions.Add(new Instruction(OpCodes.Ldloc_3));
                    instructions.Add(new Instruction(OpCodes.Ldc_I4, (int)_strings.Count));
                    instructions.Add(new Instruction(OpCodes.Blt_S, (sbyte)-28));
                    instructions.Add(new Instruction(OpCodes.Ret));
                }

                methodBody.Build(method);
            }
        }
        /// <summary>
        /// static GetMergedAssemblyNames() : [mscorlib]System.String[]
        /// </summary>
        private void GetMergedAssemblyNames(MethodDeclaration method)
        {
            method.Name        = "GetMergedAssemblyNames";
            method.Visibility  = MethodVisibilityFlags.Assembly;
            method.IsStatic    = true;
            method.IsHideBySig = true;

            // Return type
            {
                var returnType = method.ReturnType;
                returnType.Type =
                    new ArrayType(
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, _module.Assembly));
            }

            // Body
            {
                var methodBody = new MethodBody();
                methodBody.MaxStackSize = 5;
                methodBody.InitLocals   = true;

                // Local variables
                {
                    var localVariables = methodBody.LocalVariables;
                    localVariables.Add(
                        new ArrayType(
                            TypeReference.GetPrimitiveType(PrimitiveTypeCode.UInt8, _module.Assembly)));
                    localVariables.Add(
                        new TypeReference("Encoding", "System.Text",
                                          AssemblyReference.GetMscorlib(_module.Assembly), false));
                    localVariables.Add(
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.Int32, _module.Assembly));
                    localVariables.Add(
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.Int32, _module.Assembly));
                    localVariables.Add(
                        new ArrayType(
                            TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, _module.Assembly)));
                    localVariables.Add(
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.Int32, _module.Assembly));
                    localVariables.Add(
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.Object, _module.Assembly));
                }

                // Exception handlers
                {
                    var exceptionHandlers = methodBody.ExceptionHandlers;
                    exceptionHandlers.Add(
                        new ExceptionHandler(
                            ExceptionHandlerType.Finally,
                            20, 82, 102, 8, 0));
                }

                // Instructions
                {
                    var instructions = methodBody.Instructions;
                    instructions.Add(new Instruction(OpCodes.Ldsfld,
                                                     new FieldReference(
                                                         "_mergedAssemblyNames",
                                                         new ArrayType(
                                                             TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, _module.Assembly)),
                                                         new TypeReference(_mainType.Name, _mainType.Namespace, false))));
                    instructions.Add(new Instruction(OpCodes.Brtrue_S, (sbyte)103));
                    instructions.Add(new Instruction(OpCodes.Ldsfld,
                                                     new FieldReference(
                                                         "_mergeLockObject",
                                                         TypeReference.GetPrimitiveType(PrimitiveTypeCode.Object, _module.Assembly),
                                                         new TypeReference(_mainType.Name, _mainType.Namespace, false))));
                    instructions.Add(new Instruction(OpCodes.Dup));
                    instructions.Add(new Instruction(OpCodes.Stloc_S, (byte)6));
                    instructions.Add(new Instruction(OpCodes.Call,
                                                     new MethodReference(
                                                         "Enter",
                                                         new TypeReference("Monitor", "System.Threading",
                                                                           AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                         new CallSite(
                                                             false,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             TypeReference.GetPrimitiveType(PrimitiveTypeCode.Void, _module.Assembly),
                                                             new TypeSignature[]
                    {
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.Object, _module.Assembly),
                    },
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Ldsfld,
                                                     new FieldReference(
                                                         "_mergedAssemblyNames",
                                                         new ArrayType(
                                                             TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, _module.Assembly)),
                                                         new TypeReference(_mainType.Name, _mainType.Namespace, false))));
                    instructions.Add(new Instruction(OpCodes.Brtrue_S, (sbyte)73));
                    instructions.Add(new Instruction(OpCodes.Ldc_I4, (int)_dataID));
                    instructions.Add(new Instruction(OpCodes.Call,
                                                     new MethodReference(
                                                         "GetData",
                                                         new TypeReference(_mainType.Name, _mainType.Namespace, false),
                                                         new CallSite(
                                                             false,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             new ArrayType(
                                                                 TypeReference.GetPrimitiveType(PrimitiveTypeCode.UInt8, _module.Assembly)),
                                                             new TypeSignature[]
                    {
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.Int32, _module.Assembly),
                    },
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Stloc_0));
                    instructions.Add(new Instruction(OpCodes.Call,
                                                     new MethodReference(
                                                         "get_UTF8",
                                                         new TypeReference("Encoding", "System.Text",
                                                                           AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                         new CallSite(
                                                             false,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             new TypeReference("Encoding", "System.Text",
                                                                               AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                             new TypeSignature[0],
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Stloc_1));
                    instructions.Add(new Instruction(OpCodes.Ldc_I4_0));
                    instructions.Add(new Instruction(OpCodes.Stloc_2));
                    instructions.Add(new Instruction(OpCodes.Ldloc_0));
                    instructions.Add(new Instruction(OpCodes.Ldloca_S, (byte)2));
                    instructions.Add(new Instruction(OpCodes.Call,
                                                     new MethodReference(
                                                         "Read7BitEncodedInt",
                                                         new TypeReference(_mainType.Name, _mainType.Namespace, false),
                                                         new CallSite(
                                                             false,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             TypeReference.GetPrimitiveType(PrimitiveTypeCode.Int32, _module.Assembly),
                                                             new TypeSignature[]
                    {
                        new ArrayType(
                            TypeReference.GetPrimitiveType(PrimitiveTypeCode.UInt8, _module.Assembly)),
                        new ByRefType(
                            TypeReference.GetPrimitiveType(PrimitiveTypeCode.Int32, _module.Assembly)),
                    },
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Stloc_3));
                    instructions.Add(new Instruction(OpCodes.Ldloc_3));
                    instructions.Add(new Instruction(OpCodes.Newarr,
                                                     TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, _module.Assembly)));
                    instructions.Add(new Instruction(OpCodes.Stloc_S, (byte)4));
                    instructions.Add(new Instruction(OpCodes.Ldc_I4_0));
                    instructions.Add(new Instruction(OpCodes.Stloc_S, (byte)5));
                    instructions.Add(new Instruction(OpCodes.Br_S, (sbyte)20));
                    instructions.Add(new Instruction(OpCodes.Ldloc_S, (byte)4));
                    instructions.Add(new Instruction(OpCodes.Ldloc_S, (byte)5));
                    instructions.Add(new Instruction(OpCodes.Ldloc_0));
                    instructions.Add(new Instruction(OpCodes.Ldloca_S, (byte)2));
                    instructions.Add(new Instruction(OpCodes.Ldloc_1));
                    instructions.Add(new Instruction(OpCodes.Call,
                                                     new MethodReference(
                                                         "ReadString",
                                                         new TypeReference(_mainType.Name, _mainType.Namespace, false),
                                                         new CallSite(
                                                             false,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, _module.Assembly),
                                                             new TypeSignature[]
                    {
                        new ArrayType(
                            TypeReference.GetPrimitiveType(PrimitiveTypeCode.UInt8, _module.Assembly)),
                        new ByRefType(
                            TypeReference.GetPrimitiveType(PrimitiveTypeCode.Int32, _module.Assembly)),
                        new TypeReference("Encoding", "System.Text",
                                          AssemblyReference.GetMscorlib(_module.Assembly), false),
                    },
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Stelem_Ref));
                    instructions.Add(new Instruction(OpCodes.Ldloc_S, (byte)5));
                    instructions.Add(new Instruction(OpCodes.Ldc_I4_1));
                    instructions.Add(new Instruction(OpCodes.Add));
                    instructions.Add(new Instruction(OpCodes.Stloc_S, (byte)5));
                    instructions.Add(new Instruction(OpCodes.Ldloc_S, (byte)5));
                    instructions.Add(new Instruction(OpCodes.Ldloc_3));
                    instructions.Add(new Instruction(OpCodes.Blt_S, (sbyte)-25));
                    instructions.Add(new Instruction(OpCodes.Ldloc_S, (byte)4));
                    instructions.Add(new Instruction(OpCodes.Stsfld,
                                                     new FieldReference(
                                                         "_mergedAssemblyNames",
                                                         new ArrayType(
                                                             TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, _module.Assembly)),
                                                         new TypeReference(_mainType.Name, _mainType.Namespace, false))));
                    instructions.Add(new Instruction(OpCodes.Leave_S, (sbyte)8));
                    instructions.Add(new Instruction(OpCodes.Ldloc_S, (byte)6));
                    instructions.Add(new Instruction(OpCodes.Call,
                                                     new MethodReference(
                                                         "Exit",
                                                         new TypeReference("Monitor", "System.Threading",
                                                                           AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                         new CallSite(
                                                             false,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             TypeReference.GetPrimitiveType(PrimitiveTypeCode.Void, _module.Assembly),
                                                             new TypeSignature[]
                    {
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.Object, _module.Assembly),
                    },
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Endfinally));
                    instructions.Add(new Instruction(OpCodes.Ldsfld,
                                                     new FieldReference(
                                                         "_mergedAssemblyNames",
                                                         new ArrayType(
                                                             TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, _module.Assembly)),
                                                         new TypeReference(_mainType.Name, _mainType.Namespace, false))));
                    instructions.Add(new Instruction(OpCodes.Ret));
                }

                methodBody.Build(method);
            }
        }
        /// <summary>
        /// static OnMergedAssemblyResolve([mscorlib]System.Object, [mscorlib]System.ResolveEventArgs) : [mscorlib]System.Reflection.Assembly
        /// </summary>
        private void OnMergedAssemblyResolve(MethodDeclaration method)
        {
            method.Name        = "OnMergedAssemblyResolve";
            method.Visibility  = MethodVisibilityFlags.Assembly;
            method.IsStatic    = true;
            method.IsHideBySig = true;

            // Return type
            {
                var returnType = method.ReturnType;
                returnType.Type =
                    new TypeReference("Assembly", "System.Reflection",
                                      AssemblyReference.GetMscorlib(_module.Assembly), false);
            }

            // Parameters
            {
                var parameters = method.Parameters;

                // sender : [mscorlib]System.Object
                var parameter = parameters.Add();
                parameter.Type =
                    TypeReference.GetPrimitiveType(PrimitiveTypeCode.Object, _module.Assembly);

                // args : [mscorlib]System.ResolveEventArgs
                parameter      = parameters.Add();
                parameter.Type =
                    new TypeReference("ResolveEventArgs", "System",
                                      AssemblyReference.GetMscorlib(_module.Assembly), false);
            }

            // Body
            {
                var methodBody = new MethodBody();
                methodBody.MaxStackSize = 2;
                methodBody.InitLocals   = true;

                // Local variables
                {
                    var localVariables = methodBody.LocalVariables;
                    localVariables.Add(
                        new ArrayType(
                            TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, _module.Assembly)));
                    localVariables.Add(
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, _module.Assembly));
                    localVariables.Add(
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.Int32, _module.Assembly));
                }

                // Instructions
                {
                    var instructions = methodBody.Instructions;
                    instructions.Add(new Instruction(OpCodes.Call,
                                                     new MethodReference(
                                                         "GetMergedAssemblyNames",
                                                         new TypeReference(_mainType.Name, _mainType.Namespace, false),
                                                         new CallSite(
                                                             false,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             new ArrayType(
                                                                 TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, _module.Assembly)),
                                                             new TypeSignature[0],
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Stloc_0));
                    instructions.Add(new Instruction(OpCodes.Ldarg_1));
                    instructions.Add(new Instruction(OpCodes.Callvirt,
                                                     new MethodReference(
                                                         "get_Name",
                                                         new TypeReference("ResolveEventArgs", "System",
                                                                           AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                         new CallSite(
                                                             true,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, _module.Assembly),
                                                             new TypeSignature[0],
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Call,
                                                     new MethodReference(
                                                         "GetAssemblyName",
                                                         new TypeReference(_mainType.Name, _mainType.Namespace, false),
                                                         new CallSite(
                                                             false,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, _module.Assembly),
                                                             new TypeSignature[]
                    {
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, _module.Assembly),
                    },
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Stloc_1));
                    instructions.Add(new Instruction(OpCodes.Ldc_I4_0));
                    instructions.Add(new Instruction(OpCodes.Stloc_2));
                    instructions.Add(new Instruction(OpCodes.Br_S, (sbyte)21));
                    instructions.Add(new Instruction(OpCodes.Ldloc_0));
                    instructions.Add(new Instruction(OpCodes.Ldloc_2));
                    instructions.Add(new Instruction(OpCodes.Ldelem_Ref));
                    instructions.Add(new Instruction(OpCodes.Ldloc_1));
                    instructions.Add(new Instruction(OpCodes.Call,
                                                     new MethodReference(
                                                         "op_Equality",
                                                         TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, _module.Assembly),
                                                         new CallSite(
                                                             false,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             TypeReference.GetPrimitiveType(PrimitiveTypeCode.Boolean, _module.Assembly),
                                                             new TypeSignature[]
                    {
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, _module.Assembly),
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, _module.Assembly),
                    },
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Brfalse_S, (sbyte)6));
                    instructions.Add(new Instruction(OpCodes.Call,
                                                     new MethodReference(
                                                         "GetExecutingAssembly",
                                                         new TypeReference("Assembly", "System.Reflection",
                                                                           AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                         new CallSite(
                                                             false,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             new TypeReference("Assembly", "System.Reflection",
                                                                               AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                             new TypeSignature[0],
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Ret));
                    instructions.Add(new Instruction(OpCodes.Ldloc_2));
                    instructions.Add(new Instruction(OpCodes.Ldc_I4_1));
                    instructions.Add(new Instruction(OpCodes.Add));
                    instructions.Add(new Instruction(OpCodes.Stloc_2));
                    instructions.Add(new Instruction(OpCodes.Ldloc_2));
                    instructions.Add(new Instruction(OpCodes.Ldloc_0));
                    instructions.Add(new Instruction(OpCodes.Ldlen));
                    instructions.Add(new Instruction(OpCodes.Conv_I4));
                    instructions.Add(new Instruction(OpCodes.Blt_S, (sbyte)-27));
                    instructions.Add(new Instruction(OpCodes.Ldnull));
                    instructions.Add(new Instruction(OpCodes.Ret));
                }

                methodBody.Build(method);
            }
        }
        /// <summary>
        /// static MergeInitialize() : [mscorlib]System.Void
        /// </summary>
        private void MergeInitialize(MethodDeclaration method)
        {
            method.Name        = "MergeInitialize";
            method.Visibility  = MethodVisibilityFlags.Assembly;
            method.IsStatic    = true;
            method.IsHideBySig = true;

            // Return type
            {
                var returnType = method.ReturnType;
                returnType.Type =
                    TypeReference.GetPrimitiveType(PrimitiveTypeCode.Void, _module.Assembly);
            }

            // Body
            {
                var methodBody = new MethodBody();
                methodBody.MaxStackSize = 8;
                methodBody.InitLocals   = true;

                // Instructions
                {
                    var instructions = methodBody.Instructions;
                    instructions.Add(new Instruction(OpCodes.Call,
                                                     new MethodReference(
                                                         "get_CurrentDomain",
                                                         new TypeReference("AppDomain", "System",
                                                                           AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                         new CallSite(
                                                             false,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             new TypeReference("AppDomain", "System",
                                                                               AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                             new TypeSignature[0],
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Ldnull));
                    instructions.Add(new Instruction(OpCodes.Ldftn,
                                                     new MethodReference(
                                                         "OnMergedAssemblyResolve",
                                                         new TypeReference(_mainType.Name, _mainType.Namespace, false),
                                                         new CallSite(
                                                             false,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             new TypeReference("Assembly", "System.Reflection",
                                                                               AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                             new TypeSignature[]
                    {
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.Object, _module.Assembly),
                        new TypeReference("ResolveEventArgs", "System",
                                          AssemblyReference.GetMscorlib(_module.Assembly), false),
                    },
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Newobj,
                                                     new MethodReference(
                                                         CodeModelUtils.MethodConstructorName,
                                                         new TypeReference("ResolveEventHandler", "System",
                                                                           AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                         new CallSite(
                                                             true,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             TypeReference.GetPrimitiveType(PrimitiveTypeCode.Void, _module.Assembly),
                                                             new TypeSignature[]
                    {
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.Object, _module.Assembly),
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.IntPtr, _module.Assembly),
                    },
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Callvirt,
                                                     new MethodReference(
                                                         "add_AssemblyResolve",
                                                         new TypeReference("AppDomain", "System",
                                                                           AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                         new CallSite(
                                                             true,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             TypeReference.GetPrimitiveType(PrimitiveTypeCode.Void, _module.Assembly),
                                                             new TypeSignature[]
                    {
                        new TypeReference("ResolveEventHandler", "System",
                                          AssemblyReference.GetMscorlib(_module.Assembly), false),
                    },
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Ret));
                }

                methodBody.Build(method);
            }
        }
Esempio n. 12
0
        public override void Build(CustomAttribute customAttribute)
        {
            customAttribute.Constructor =
                new MethodReference(
                    ".ctor",
                    new TypeReference(
                        "SuppressMessageAttribute",
                        "System.Diagnostics.CodeAnalysis",
                        AssemblyReference.GetMscorlib(customAttribute.Assembly)),
                    new CallSite(
                        true,
                        false,
                        MethodCallingConvention.Default,
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.Void, customAttribute.Assembly),
                        new TypeSignature[]
            {
                TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, customAttribute.Assembly),
                TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, customAttribute.Assembly),
            },
                        0,
                        0));

            // Ctor arguments
            var ctorArguments = customAttribute.CtorArguments;

            ctorArguments.Clear();

            // Category
            ctorArguments.Add(
                new CustomAttributeTypedArgument(
                    _category,
                    TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, customAttribute.Assembly)));

            // CheckId
            ctorArguments.Add(
                new CustomAttributeTypedArgument(
                    _checkId,
                    TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, customAttribute.Assembly)));

            // Named arguments
            var namedArguments = customAttribute.NamedArguments;

            namedArguments.Clear();

            // Justification
            if (!string.IsNullOrEmpty(_justification))
            {
                namedArguments.Add(
                    new CustomAttributeNamedArgument(
                        "Justification",
                        CustomAttributeNamedArgumentType.Property,
                        new CustomAttributeTypedArgument(
                            _justification,
                            TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, customAttribute.Assembly))));
            }

            // MessageId
            if (!string.IsNullOrEmpty(_messageId))
            {
                namedArguments.Add(
                    new CustomAttributeNamedArgument(
                        "MessageId",
                        CustomAttributeNamedArgumentType.Property,
                        new CustomAttributeTypedArgument(
                            _messageId,
                            TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, customAttribute.Assembly))));
            }

            // Scope
            if (!string.IsNullOrEmpty(_scope))
            {
                namedArguments.Add(
                    new CustomAttributeNamedArgument(
                        "Scope",
                        CustomAttributeNamedArgumentType.Property,
                        new CustomAttributeTypedArgument(
                            _scope,
                            TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, customAttribute.Assembly))));
            }

            // Target
            if (!string.IsNullOrEmpty(_target))
            {
                namedArguments.Add(
                    new CustomAttributeNamedArgument(
                        "Target",
                        CustomAttributeNamedArgumentType.Property,
                        new CustomAttributeTypedArgument(
                            _target,
                            TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, customAttribute.Assembly))));
            }
        }
        /// <summary>
        /// static InitializeMethodPointers() : [mscorlib]System.Void
        /// </summary>
        private void GenerateInitializeMethod()
        {
            var method = _mainType.Methods.Add();

            method.Name        = "InitializeMethodPointers";
            method.Visibility  = MethodVisibilityFlags.Assembly;
            method.IsStatic    = true;
            method.IsHideBySig = true;

            // Return type
            {
                var returnType = method.ReturnType;
                returnType.Type =
                    TypeReference.GetPrimitiveType(PrimitiveTypeCode.Void, _module.Assembly);
            }

            // Body
            {
                var methodBody = new MethodBody();
                methodBody.MaxStackSize = 7;
                methodBody.InitLocals   = true;

                // Local variables
                {
                    var localVariables = methodBody.LocalVariables;
                    localVariables.Add(
                        new TypeReference("Module", "System.Reflection",
                                          AssemblyReference.GetMscorlib(_module.Assembly), false));
                    localVariables.Add(
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.Int32, _module.Assembly));
                    localVariables.Add(
                        new ArrayType(
                            TypeReference.GetPrimitiveType(PrimitiveTypeCode.UInt8, _module.Assembly)));
                    localVariables.Add(
                        new ArrayType(
                            TypeReference.GetPrimitiveType(PrimitiveTypeCode.Type, _module.Assembly)));
                    localVariables.Add(
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.Int32, _module.Assembly));
                    localVariables.Add(
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.Int32, _module.Assembly));
                    localVariables.Add(
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.Object, _module.Assembly));
                    localVariables.Add(
                        new TypeReference("RuntimeMethodHandle", "System",
                                          AssemblyReference.GetMscorlib(_module.Assembly), true));
                }

                // Exception handlers
                {
                    var exceptionHandlers = methodBody.ExceptionHandlers;
                    exceptionHandlers.Add(
                        new ExceptionHandler(
                            ExceptionHandlerType.Finally,
                            44, 64, 108, 8, 0));
                }

                // Instructions
                {
                    var instructions = methodBody.Instructions;
                    instructions.Add(new Instruction(OpCodes.Ldtoken,
                                                     new TypeReference(_mainType.Name, _mainType.Namespace, false)));
                    instructions.Add(new Instruction(OpCodes.Call,
                                                     new MethodReference(
                                                         "GetTypeFromHandle",
                                                         TypeReference.GetPrimitiveType(PrimitiveTypeCode.Type, _module.Assembly),
                                                         new CallSite(
                                                             false,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             TypeReference.GetPrimitiveType(PrimitiveTypeCode.Type, _module.Assembly),
                                                             new TypeSignature[]
                    {
                        new TypeReference("RuntimeTypeHandle", "System",
                                          AssemblyReference.GetMscorlib(_module.Assembly), true),
                    },
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Callvirt,
                                                     new MethodReference(
                                                         "get_Module",
                                                         TypeReference.GetPrimitiveType(PrimitiveTypeCode.Type, _module.Assembly),
                                                         new CallSite(
                                                             true,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             new TypeReference("Module", "System.Reflection",
                                                                               AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                             new TypeSignature[0],
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Stloc_0));
                    instructions.Add(new Instruction(OpCodes.Ldc_I4, (int)_signatures.Length));
                    instructions.Add(new Instruction(OpCodes.Stloc_1));
                    instructions.Add(new Instruction(OpCodes.Ldloc_1));
                    instructions.Add(new Instruction(OpCodes.Ldc_I4_4));
                    instructions.Add(new Instruction(OpCodes.Mul));
                    instructions.Add(new Instruction(OpCodes.Newarr,
                                                     TypeReference.GetPrimitiveType(PrimitiveTypeCode.UInt8, _module.Assembly)));
                    instructions.Add(new Instruction(OpCodes.Stloc_2));
                    instructions.Add(new Instruction(OpCodes.Ldsfld,
                                                     new FieldReference(
                                                         "_dataLockObject",
                                                         TypeReference.GetPrimitiveType(PrimitiveTypeCode.Object, _module.Assembly),
                                                         new TypeReference(_mainType.Name, _mainType.Namespace, false))));
                    instructions.Add(new Instruction(OpCodes.Dup));
                    instructions.Add(new Instruction(OpCodes.Stloc_S, (byte)6));
                    instructions.Add(new Instruction(OpCodes.Call,
                                                     new MethodReference(
                                                         "Enter",
                                                         new TypeReference("Monitor", "System.Threading",
                                                                           AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                         new CallSite(
                                                             false,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             TypeReference.GetPrimitiveType(PrimitiveTypeCode.Void, _module.Assembly),
                                                             new TypeSignature[]
                    {
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.Object, _module.Assembly),
                    },
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Ldtoken,
                                                     new TypeReference(_mainType.Name, _mainType.Namespace, false)));
                    instructions.Add(new Instruction(OpCodes.Call,
                                                     new MethodReference(
                                                         "GetTypeFromHandle",
                                                         TypeReference.GetPrimitiveType(PrimitiveTypeCode.Type, _module.Assembly),
                                                         new CallSite(
                                                             false,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             TypeReference.GetPrimitiveType(PrimitiveTypeCode.Type, _module.Assembly),
                                                             new TypeSignature[]
                    {
                        new TypeReference("RuntimeTypeHandle", "System",
                                          AssemblyReference.GetMscorlib(_module.Assembly), true),
                    },
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Callvirt,
                                                     new MethodReference(
                                                         "get_Assembly",
                                                         TypeReference.GetPrimitiveType(PrimitiveTypeCode.Type, _module.Assembly),
                                                         new CallSite(
                                                             true,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             new TypeReference("Assembly", "System.Reflection",
                                                                               AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                             new TypeSignature[0],
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Ldstr, _resourceStorage.ResourceName));
                    instructions.Add(new Instruction(OpCodes.Callvirt,
                                                     new MethodReference(
                                                         "GetManifestResourceStream",
                                                         new TypeReference("Assembly", "System.Reflection",
                                                                           AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                         new CallSite(
                                                             true,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             new TypeReference("Stream", "System.IO",
                                                                               AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                             new TypeSignature[]
                    {
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.String, _module.Assembly),
                    },
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Stsfld,
                                                     new FieldReference(
                                                         "_dataStream",
                                                         new TypeReference("Stream", "System.IO",
                                                                           AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                         new TypeReference(_mainType.Name, _mainType.Namespace, false))));
                    instructions.Add(new Instruction(OpCodes.Ldsfld,
                                                     new FieldReference(
                                                         "_dataStream",
                                                         new TypeReference("Stream", "System.IO",
                                                                           AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                         new TypeReference(_mainType.Name, _mainType.Namespace, false))));
                    instructions.Add(new Instruction(OpCodes.Ldc_I4, (int)_dataOffset));
                    instructions.Add(new Instruction(OpCodes.Conv_I8));
                    instructions.Add(new Instruction(OpCodes.Callvirt,
                                                     new MethodReference(
                                                         "set_Position",
                                                         new TypeReference("Stream", "System.IO",
                                                                           AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                         new CallSite(
                                                             true,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             TypeReference.GetPrimitiveType(PrimitiveTypeCode.Void, _module.Assembly),
                                                             new TypeSignature[]
                    {
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.Int64, _module.Assembly),
                    },
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Ldsfld,
                                                     new FieldReference(
                                                         "_dataStream",
                                                         new TypeReference("Stream", "System.IO",
                                                                           AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                         new TypeReference(_mainType.Name, _mainType.Namespace, false))));
                    instructions.Add(new Instruction(OpCodes.Ldloc_2));
                    instructions.Add(new Instruction(OpCodes.Ldc_I4_0));
                    instructions.Add(new Instruction(OpCodes.Ldloc_2));
                    instructions.Add(new Instruction(OpCodes.Ldlen));
                    instructions.Add(new Instruction(OpCodes.Conv_I4));
                    instructions.Add(new Instruction(OpCodes.Callvirt,
                                                     new MethodReference(
                                                         "Read",
                                                         new TypeReference("Stream", "System.IO",
                                                                           AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                         new CallSite(
                                                             true,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             TypeReference.GetPrimitiveType(PrimitiveTypeCode.Int32, _module.Assembly),
                                                             new TypeSignature[]
                    {
                        new ArrayType(
                            TypeReference.GetPrimitiveType(PrimitiveTypeCode.UInt8, _module.Assembly)),
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.Int32, _module.Assembly),
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.Int32, _module.Assembly),
                    },
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Pop));
                    instructions.Add(new Instruction(OpCodes.Leave_S, (sbyte)8));
                    instructions.Add(new Instruction(OpCodes.Ldloc_S, (byte)6));
                    instructions.Add(new Instruction(OpCodes.Call,
                                                     new MethodReference(
                                                         "Exit",
                                                         new TypeReference("Monitor", "System.Threading",
                                                                           AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                         new CallSite(
                                                             false,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             TypeReference.GetPrimitiveType(PrimitiveTypeCode.Void, _module.Assembly),
                                                             new TypeSignature[]
                    {
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.Object, _module.Assembly),
                    },
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Endfinally));
                    instructions.Add(new Instruction(OpCodes.Ldloc_1));
                    instructions.Add(new Instruction(OpCodes.Newarr,
                                                     TypeReference.GetPrimitiveType(PrimitiveTypeCode.IntPtr, _module.Assembly)));
                    instructions.Add(new Instruction(OpCodes.Stsfld,
                                                     new FieldReference(
                                                         "MethodPointers",
                                                         new ArrayType(
                                                             TypeReference.GetPrimitiveType(PrimitiveTypeCode.IntPtr, _module.Assembly)),
                                                         new TypeReference(_mainType.Name, _mainType.Namespace, false))));
                    instructions.Add(new Instruction(OpCodes.Ldsfld,
                                                     new FieldReference(
                                                         "EmptyTypes",
                                                         new ArrayType(
                                                             TypeReference.GetPrimitiveType(PrimitiveTypeCode.Type, _module.Assembly)),
                                                         TypeReference.GetPrimitiveType(PrimitiveTypeCode.Type, _module.Assembly))));
                    instructions.Add(new Instruction(OpCodes.Stloc_3));
                    instructions.Add(new Instruction(OpCodes.Ldc_I4_0));
                    instructions.Add(new Instruction(OpCodes.Stloc_S, (byte)4));
                    instructions.Add(new Instruction(OpCodes.Ldc_I4_0));
                    instructions.Add(new Instruction(OpCodes.Stloc_S, (byte)5));
                    instructions.Add(new Instruction(OpCodes.Br_S, (sbyte)92));
                    instructions.Add(new Instruction(OpCodes.Ldsfld,
                                                     new FieldReference(
                                                         "MethodPointers",
                                                         new ArrayType(
                                                             TypeReference.GetPrimitiveType(PrimitiveTypeCode.IntPtr, _module.Assembly)),
                                                         new TypeReference(_mainType.Name, _mainType.Namespace, false))));
                    instructions.Add(new Instruction(OpCodes.Ldloc_S, (byte)5));
                    instructions.Add(new Instruction(OpCodes.Ldelema,
                                                     TypeReference.GetPrimitiveType(PrimitiveTypeCode.IntPtr, _module.Assembly)));
                    instructions.Add(new Instruction(OpCodes.Ldloc_0));
                    instructions.Add(new Instruction(OpCodes.Ldloc_2));
                    instructions.Add(new Instruction(OpCodes.Ldloc_S, (byte)4));
                    instructions.Add(new Instruction(OpCodes.Dup));
                    instructions.Add(new Instruction(OpCodes.Ldc_I4_1));
                    instructions.Add(new Instruction(OpCodes.Add));
                    instructions.Add(new Instruction(OpCodes.Stloc_S, (byte)4));
                    instructions.Add(new Instruction(OpCodes.Ldelem_U1));
                    instructions.Add(new Instruction(OpCodes.Ldloc_2));
                    instructions.Add(new Instruction(OpCodes.Ldloc_S, (byte)4));
                    instructions.Add(new Instruction(OpCodes.Dup));
                    instructions.Add(new Instruction(OpCodes.Ldc_I4_1));
                    instructions.Add(new Instruction(OpCodes.Add));
                    instructions.Add(new Instruction(OpCodes.Stloc_S, (byte)4));
                    instructions.Add(new Instruction(OpCodes.Ldelem_U1));
                    instructions.Add(new Instruction(OpCodes.Ldc_I4_8));
                    instructions.Add(new Instruction(OpCodes.Shl));
                    instructions.Add(new Instruction(OpCodes.Or));
                    instructions.Add(new Instruction(OpCodes.Ldloc_2));
                    instructions.Add(new Instruction(OpCodes.Ldloc_S, (byte)4));
                    instructions.Add(new Instruction(OpCodes.Dup));
                    instructions.Add(new Instruction(OpCodes.Ldc_I4_1));
                    instructions.Add(new Instruction(OpCodes.Add));
                    instructions.Add(new Instruction(OpCodes.Stloc_S, (byte)4));
                    instructions.Add(new Instruction(OpCodes.Ldelem_U1));
                    instructions.Add(new Instruction(OpCodes.Ldc_I4_S, (byte)16));
                    instructions.Add(new Instruction(OpCodes.Shl));
                    instructions.Add(new Instruction(OpCodes.Or));
                    instructions.Add(new Instruction(OpCodes.Ldloc_2));
                    instructions.Add(new Instruction(OpCodes.Ldloc_S, (byte)4));
                    instructions.Add(new Instruction(OpCodes.Dup));
                    instructions.Add(new Instruction(OpCodes.Ldc_I4_1));
                    instructions.Add(new Instruction(OpCodes.Add));
                    instructions.Add(new Instruction(OpCodes.Stloc_S, (byte)4));
                    instructions.Add(new Instruction(OpCodes.Ldelem_U1));
                    instructions.Add(new Instruction(OpCodes.Ldc_I4_S, (byte)24));
                    instructions.Add(new Instruction(OpCodes.Shl));
                    instructions.Add(new Instruction(OpCodes.Or));
                    instructions.Add(new Instruction(OpCodes.Ldloc_3));
                    instructions.Add(new Instruction(OpCodes.Ldloc_3));
                    instructions.Add(new Instruction(OpCodes.Callvirt,
                                                     new MethodReference(
                                                         "ResolveMethod",
                                                         new TypeReference("Module", "System.Reflection",
                                                                           AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                         new CallSite(
                                                             true,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             new TypeReference("MethodBase", "System.Reflection",
                                                                               AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                             new TypeSignature[]
                    {
                        TypeReference.GetPrimitiveType(PrimitiveTypeCode.Int32, _module.Assembly),
                        new ArrayType(
                            TypeReference.GetPrimitiveType(PrimitiveTypeCode.Type, _module.Assembly)),
                        new ArrayType(
                            TypeReference.GetPrimitiveType(PrimitiveTypeCode.Type, _module.Assembly)),
                    },
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Callvirt,
                                                     new MethodReference(
                                                         "get_MethodHandle",
                                                         new TypeReference("MethodBase", "System.Reflection",
                                                                           AssemblyReference.GetMscorlib(_module.Assembly), false),
                                                         new CallSite(
                                                             true,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             new TypeReference("RuntimeMethodHandle", "System",
                                                                               AssemblyReference.GetMscorlib(_module.Assembly), true),
                                                             new TypeSignature[0],
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Stloc_S, (byte)7));
                    instructions.Add(new Instruction(OpCodes.Ldloca_S, (byte)7));
                    instructions.Add(new Instruction(OpCodes.Call,
                                                     new MethodReference(
                                                         "GetFunctionPointer",
                                                         new TypeReference("RuntimeMethodHandle", "System",
                                                                           AssemblyReference.GetMscorlib(_module.Assembly), true),
                                                         new CallSite(
                                                             true,
                                                             false,
                                                             MethodCallingConvention.Default,
                                                             TypeReference.GetPrimitiveType(PrimitiveTypeCode.IntPtr, _module.Assembly),
                                                             new TypeSignature[0],
                                                             -1,
                                                             0))));
                    instructions.Add(new Instruction(OpCodes.Stobj,
                                                     TypeReference.GetPrimitiveType(PrimitiveTypeCode.IntPtr, _module.Assembly)));
                    instructions.Add(new Instruction(OpCodes.Ldloc_S, (byte)5));
                    instructions.Add(new Instruction(OpCodes.Ldc_I4_1));
                    instructions.Add(new Instruction(OpCodes.Add));
                    instructions.Add(new Instruction(OpCodes.Stloc_S, (byte)5));
                    instructions.Add(new Instruction(OpCodes.Ldloc_S, (byte)5));
                    instructions.Add(new Instruction(OpCodes.Ldloc_1));
                    instructions.Add(new Instruction(OpCodes.Blt_S, (sbyte)-97));
                    instructions.Add(new Instruction(OpCodes.Ret));
                }

                methodBody.Build(method);
            }
        }