Exemplo n.º 1
0
    internal MyConstructorBuilder()
    {
// <Snippet1>
        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", true);
        FieldInfo myFieldInfo =
            myModuleBuilder.DefineUninitializedData("myField", 2, FieldAttributes.Public);
        // 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) };
        // Define a constructor of the dynamic class.
        ConstructorBuilder myConstructor = myTypeBuilder.DefineConstructor(
            MethodAttributes.Public, CallingConventions.Standard, myConstructorArgs);

        // Display the name of the constructor.
        Console.WriteLine("The constructor name is  : " + myConstructor.Name);
        myConstructor.SetSymCustomAttribute("MySimAttribute", new byte[] { 01, 00, 00 });
// </Snippet1>
        // Generate the IL for the method and call its superclass constructor.
        ILGenerator myILGenerator3 = myConstructor.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();
    }