public void TestMethodImplementationFlags() { ConstructorBuilder cb = genClass.DefineConstructor( 0, 0, new Type [0]); Assert.AreEqual(MethodImplAttributes.Managed | MethodImplAttributes.IL, cb.GetMethodImplementationFlags(), "#A1"); cb.SetImplementationFlags(MethodImplAttributes.OPTIL); Assert.AreEqual(MethodImplAttributes.OPTIL, cb.GetMethodImplementationFlags(), "#A2"); // Can not be called on a created type TypeBuilder tb = module.DefineType(genTypeName(), TypeAttributes.Public); ConstructorBuilder cb2 = tb.DefineConstructor( 0, 0, new Type [0]); cb2.GetILGenerator().Emit(OpCodes.Ret); cb2.SetImplementationFlags(MethodImplAttributes.Managed); tb.CreateType(); try { cb2.SetImplementationFlags(MethodImplAttributes.OPTIL); Assert.Fail("#B1"); } catch (InvalidOperationException ex) { // Unable to change after type has been created Assert.AreEqual(typeof(InvalidOperationException), ex.GetType(), "#B2"); Assert.IsNull(ex.InnerException, "#B3"); Assert.IsNotNull(ex.Message, "#B4"); } }
/// <summary> /// 发行。 /// </summary> public void Emit(ConstructorBuilder builder) { #if NET40 var attributes = builder.GetMethodImplementationFlags(); #else var attributes = builder.MethodImplementationFlags; #endif if ((attributes & MethodImplAttributes.Runtime) != MethodImplAttributes.IL) { return; } if (IsEmpty) { InvokeBaseConstructor(); } foreach (var item in parameters) { item.Emit(builder.DefineParameter(item.Position, item.Attributes, item.ParameterName)); } var ilg = builder.GetILGenerator(); base.Load(ilg); ilg.Emit(OpCodes.Ret); }
/// <summary> /// 发行。 /// </summary> internal void Emit(ConstructorBuilder builder) { #if NET40 var attributes = builder.GetMethodImplementationFlags(); #else var attributes = builder.MethodImplementationFlags; #endif if ((attributes & MethodImplAttributes.Runtime) != MethodImplAttributes.IL) { return; } var ilg = builder.GetILGenerator(); base.Load(ilg); ilg.Emit(OpCodes.Ret); }
internal MyConstructorBuilder() { try { // <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 myFieldInfo2 = 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); // Set the method implementation flags for the constructor. myConstructor.SetImplementationFlags(MethodImplAttributes.PreserveSig | MethodImplAttributes.Runtime); // Get the method implementation flags for the constructor. MethodImplAttributes myMethodAttributes = myConstructor.GetMethodImplementationFlags(); Type myAttributeType = typeof(MethodImplAttributes); int myAttribValue = (int)myMethodAttributes; if (!myAttributeType.IsEnum) { Console.WriteLine("This is not an Enum"); } // Display the field info names of the retrieved method implementation flags. FieldInfo[] myFieldInfo = myAttributeType.GetFields(BindingFlags.Public | BindingFlags.Static); Console.WriteLine("The Field info names of the MethodImplAttributes for the constructor are:"); for (int i = 0; i < myFieldInfo.Length; i++) { int myFieldValue = (Int32)myFieldInfo[i].GetValue(null); if ((myFieldValue & myAttribValue) == myFieldValue) { Console.WriteLine(" " + myFieldInfo[i].Name); } } // </Snippet1> // 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(); } catch (InvalidOperationException ex) { Console.WriteLine("The following exception has occurred : " + ex.Message); } catch (Exception ex) { Console.WriteLine("The following exception has occurred : " + ex.Message); } }
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> }