Пример #1
0
        public void GetModule()
        {
            ConstructorBuilder cb = genClass.DefineConstructor(
                0, 0, new Type [0]);

            Assert.AreSame(module, cb.GetModule());
        }
Пример #2
0
    internal MyConstructorBuilder()
    {
// <Snippet1>
// <Snippet3>

        MethodBuilder myMethodBuilder = null;
        AppDomain     myCurrentDomain = AppDomain.CurrentDomain;
        // Create assembly in current CurrentDomain.
        AssemblyName myAssemblyName = new AssemblyName();

        myAssemblyName.Name = "TempAssembly";
        // Create a dynamic assembly.
        myAssemblyBuilder = myCurrentDomain.DefineDynamicAssembly
                                (myAssemblyName, AssemblyBuilderAccess.Run);
        // Create a dynamic module in the assembly.
        myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule");
        // Create a type in the module.
        TypeBuilder  myTypeBuilder   = myModuleBuilder.DefineType("TempClass", TypeAttributes.Public);
        FieldBuilder myGreetingField = myTypeBuilder.DefineField("Greeting",
                                                                 typeof(String), FieldAttributes.Public);

        Type[] myConstructorArgs = { typeof(String) };
// <Snippet2>
// <Snippet4>
        // Define a constructor of the dynamic class.
        ConstructorBuilder myConstructorBuilder = myTypeBuilder.DefineConstructor(
            MethodAttributes.Public, CallingConventions.Standard, myConstructorArgs);
        // Get a reference to the module that contains this constructor.
        Module myModule = myConstructorBuilder.GetModule();

        Console.WriteLine("Module Name : " + myModule.Name);
        // Get the 'MethodToken' that represents the token for this constructor.
        MethodToken myMethodToken = myConstructorBuilder.GetToken();

        Console.WriteLine("Constructor Token is : " + myMethodToken.Token);
        // Get the method implementation flags for this constructor.
        MethodImplAttributes myMethodImplAttributes = myConstructorBuilder.GetMethodImplementationFlags();

        Console.WriteLine("MethodImplAttributes : " + myMethodImplAttributes);
// </Snippet3>
// </Snippet2>
// </Snippet1>
        // Generate IL for the method, call its base class constructor and store the arguments
        // in the private field.
        ILGenerator myILGenerator3 = myConstructorBuilder.GetILGenerator();

        myILGenerator3.Emit(OpCodes.Ldarg_0);
        ConstructorInfo myConstructorInfo = typeof(Object).GetConstructor(new Type[0]);

        myILGenerator3.Emit(OpCodes.Call, myConstructorInfo);
        myILGenerator3.Emit(OpCodes.Ldarg_0);
        myILGenerator3.Emit(OpCodes.Ldarg_1);
        myILGenerator3.Emit(OpCodes.Stfld, myGreetingField);
        myILGenerator3.Emit(OpCodes.Ret);
        // Add a method to the type.
        myMethodBuilder = myTypeBuilder.DefineMethod
                              ("HelloWorld", MethodAttributes.Public, null, null);
        // Generate IL for the method.
        ILGenerator myILGenerator2 = myMethodBuilder.GetILGenerator();

        myILGenerator2.EmitWriteLine("Hello World from global");
        myILGenerator2.Emit(OpCodes.Ret);
        myModuleBuilder.CreateGlobalFunctions();
        myType1 = myTypeBuilder.CreateType();

        // Get the parameters of this constructor.
        ParameterInfo[] myParameterInfo = myConstructorBuilder.GetParameters();
        for (int i = 0; i < myParameterInfo.Length; i++)
        {
            Console.WriteLine("Declaration type : " + myParameterInfo[i].Member.DeclaringType);
        }
// </Snippet4>
    }