public MyConstructorBuilder() { // <Snippet2> 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"); 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); // Display the 'Type' object from which this object was obtained. Console.WriteLine("The reflected type is : " + myConstructor.ReflectedType); // Display the signature of the field. Console.WriteLine(myConstructor.Signature); // Display the constructor builder instance as a string. Console.WriteLine(myConstructor.ToString()); // </Snippet2> // Generate IL for the method, call its superclass constructor and store the arguments // in the private field. 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(); }
public void ToString_NoRequiredOptionalCustomModifiers() { TypeBuilder type = Helpers.DynamicType(TypeAttributes.Public); Type[] parameterTypes = new Type[] { typeof(int), typeof(double) }; ConstructorBuilder constructor = type.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, parameterTypes); Assert.StartsWith("Name: .ctor", constructor.ToString()); }
public void TestToStringWithDifferentOverload() { AssemblyName an = new AssemblyName(); an.Name = "DynamicRandomAssembly"; AssemblyBuilder ab = AssemblyBuilder.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run); ModuleBuilder mb = TestLibrary.Utilities.GetModuleBuilder(ab, "Module1"); TypeBuilder tb = mb.DefineType("DynamicRandomClass", TypeAttributes.Public); Type[] parameterTypes = { typeof(int), typeof(double) }; ConstructorBuilder cb = tb.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, parameterTypes); Assert.StartsWith("Name: .ctor", cb.ToString()); }