當前位置: 首頁>>代碼示例>>C#>>正文


C# AppDomain.DefineDynamicAssembly方法代碼示例

本文整理匯總了C#中AppDomain.DefineDynamicAssembly方法的典型用法代碼示例。如果您正苦於以下問題:C# AppDomain.DefineDynamicAssembly方法的具體用法?C# AppDomain.DefineDynamicAssembly怎麽用?C# AppDomain.DefineDynamicAssembly使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在AppDomain的用法示例。


在下文中一共展示了AppDomain.DefineDynamicAssembly方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: DefineDynamicAssembly

	static Assembly DefineDynamicAssembly (AppDomain domain)
	{
		AssemblyName assemblyName = new AssemblyName ();
		assemblyName.Name = "MyDynamicAssembly";

		AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly (assemblyName, AssemblyBuilderAccess.Run);
		ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule ("MyDynamicModule");
		TypeBuilder typeBuilder = moduleBuilder.DefineType ("MyDynamicType", TypeAttributes.Public);
		ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor (MethodAttributes.Public, CallingConventions.Standard, null);
		ILGenerator ilGenerator = constructorBuilder.GetILGenerator ();
		ilGenerator.EmitWriteLine ("MyDynamicType instantiated!");
		ilGenerator.Emit (OpCodes.Ret);
		typeBuilder.CreateType ();
		return assemblyBuilder;
	}
開發者ID:Zman0169,項目名稱:mono,代碼行數:15,代碼來源:assembly_append_ordering.cs

示例2: MyCreateCallee

   private static Type MyCreateCallee(AppDomain domain)
   {
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "EmittedAssembly";
      // Define a dynamic assembly in the current application domain.
      AssemblyBuilder myAssembly =
                  domain.DefineDynamicAssembly(myAssemblyName,AssemblyBuilderAccess.Run);
      // Define a dynamic module in this assembly.
      ModuleBuilder myModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule");
      // Construct a 'TypeBuilder' given the name and attributes.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("HelloWorld",
         TypeAttributes.Public);
      // Define a constructor of the dynamic class.
      ConstructorBuilder myConstructor = myTypeBuilder.DefineConstructor(
               MethodAttributes.Public, CallingConventions.Standard, new Type[]{typeof(String)});
      ILGenerator myILGenerator = myConstructor.GetILGenerator();
      myILGenerator.Emit(OpCodes.Ldstr, "Constructor is invoked");
      myILGenerator.Emit(OpCodes.Ldarg_1);
      MethodInfo myMethodInfo =
                     typeof(Console).GetMethod("WriteLine",new Type[]{typeof(string)});
      myILGenerator.Emit(OpCodes.Call, myMethodInfo);
      myILGenerator.Emit(OpCodes.Ret);
      Type myType = typeof(MyAttribute);
      ConstructorInfo myConstructorInfo = myType.GetConstructor(new Type[]{typeof(object)});
      try
      {
        CustomAttributeBuilder methodCABuilder = new CustomAttributeBuilder (myConstructorInfo, new object [] { TypeCode.Double } );        

         myConstructor.SetCustomAttribute(methodCABuilder);
      }
      catch(ArgumentNullException ex)
      {
         Console.WriteLine("The following exception has occured : "+ex.Message);
      }
      catch(Exception ex)
      {
         Console.WriteLine("The following exception has occured : "+ex.Message);
      }
      return myTypeBuilder.CreateType();
   }
開發者ID:nobled,項目名稱:mono,代碼行數:40,代碼來源:test-295.cs

示例3: CreateCaller

  } //CreateCallee()

  // Create the caller transient dynamic assembly.
  private static Type CreateCaller(AppDomain appDomain, 
                                   AssemblyBuilderAccess access, 
                                   Type helloWorldClass) 
  {
    // Create a simple name for the caller assembly.
    AssemblyName assemblyName = new AssemblyName();
    assemblyName.Name = CALLINGASSEMBLYNAME;

    // Create the caller dynamic assembly.
    AssemblyBuilder assembly = appDomain.DefineDynamicAssembly(assemblyName, access);

    // Create a dynamic module named "CallerModule" in the caller assembly.
    ModuleBuilder module;
    if (access == AssemblyBuilderAccess.Run) 
    {
      module = assembly.DefineDynamicModule("EmittedCallerModule");
    } 
    else 
    {
      module = assembly.DefineDynamicModule("EmittedCallerModule", "EmittedCallerModule.exe");
    }

    // Define a public class named MainClass.
    TypeBuilder mainClass = module.DefineType("MainClass", TypeAttributes.Public);

    // Create the method with name "main".
    MethodAttributes methodAttributes = (MethodAttributes.Static | MethodAttributes.Public);
    MethodBuilder mainMethod = mainClass.DefineMethod("Main", methodAttributes, null, null);

    // Generate IL for the method.
    ILGenerator mainIL = mainMethod.GetILGenerator();

    // Define the greeting string constant and emit it.  This gets consumed by the constructor.
    mainIL.Emit(OpCodes.Ldstr, "HelloWorld from dynamically created caller");

    // Use the provided "HelloWorld" class
    // Find the constructor for the "HelloWorld" class.
    Type[] constructorArgs = { typeof(string) };
    ConstructorInfo constructor = helloWorldClass.GetConstructor(constructorArgs);

    // Instantiate the "HelloWorld" class.
    mainIL.Emit(OpCodes.Newobj, constructor);

    // Find the "GetGreeting" method of the "HelloWorld" class.
    MethodInfo getGreetingMethod = helloWorldClass.GetMethod("GetGreeting");

    // Call the "GetGreeting" method to obtain the greeting.
    mainIL.Emit(OpCodes.Call, getGreetingMethod);

    // Write the greeting  to the console.
    MethodInfo writeLineMethod = typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) });
    mainIL.Emit(OpCodes.Call, writeLineMethod);
    //mainIL.EmitWriteLine(
    mainIL.Emit(OpCodes.Ret);

    // Bake the class. You can now create instances of this class.
    return(mainClass.CreateType());
  } //CreateCaller()
開發者ID:gbarnett,項目名稱:shared-source-cli-2.0,代碼行數:61,代碼來源:reflectionemit.cs

示例4: CreateCallee

  } //Main()

  // Create the callee transient dynamic assembly.
  private static Type CreateCallee(AppDomain appDomain, AssemblyBuilderAccess access) 
  {
    // Create a simple name for the callee assembly.
    AssemblyName assemblyName = new AssemblyName();
    assemblyName.Name = EMITTEDASSEMBLYNAME;

    // Create the callee dynamic assembly.
    AssemblyBuilder assembly = appDomain.DefineDynamicAssembly(assemblyName, access);

    // Create a dynamic module named "CalleeModule" in the callee assembly.
    ModuleBuilder module;
    if (access == AssemblyBuilderAccess.Run) 
    {
      module = assembly.DefineDynamicModule("EmittedModule");
    } 
    else 
    {
      module = assembly.DefineDynamicModule("EmittedModule", "EmittedModule.mod");
    }

    // Define a public class named "HelloWorld" in the assembly.
    TypeBuilder helloWorldClass = module.DefineType("HelloWorld", TypeAttributes.Public);

    // Define a private string field named "Greeting" in the type.
    FieldBuilder greetingField = helloWorldClass.DefineField("Greeting", 
                                                              typeof(string), 
                                                              FieldAttributes.Private);

    // Create the constructor.  It requires a string which it stores in greetingField.
    Type[] constructorArgs = { typeof(string) };
    ConstructorBuilder constructor = helloWorldClass.DefineConstructor(
      MethodAttributes.Public, CallingConventions.Standard, constructorArgs);

    // Generate IL for the method. The constructor calls its superclass
    // constructor. The constructor stores its argument in the private field.
    ILGenerator constructorIL = constructor.GetILGenerator();
    constructorIL.Emit(OpCodes.Ldarg_0);
    ConstructorInfo superConstructor = typeof(Object).GetConstructor(new Type[0]);
    constructorIL.Emit(OpCodes.Call, superConstructor);
    constructorIL.Emit(OpCodes.Ldarg_0);
    constructorIL.Emit(OpCodes.Ldarg_1);
    // Store string argument in field.
    constructorIL.Emit(OpCodes.Stfld, greetingField);
    constructorIL.Emit(OpCodes.Ret);

    // Create the GetGreeting method.
    MethodBuilder getGreetingMethod = helloWorldClass.DefineMethod("GetGreeting", 
                                      MethodAttributes.Public, 
                                      typeof(string), 
                                      null);

    // Generate IL for GetGreeting.
    ILGenerator methodIL = getGreetingMethod.GetILGenerator();
    methodIL.Emit(OpCodes.Ldarg_0);
    methodIL.Emit(OpCodes.Ldfld, greetingField);
    methodIL.Emit(OpCodes.Ret);

    // Bake the class HelloWorld.
    return(helloWorldClass.CreateType());
  } //CreateCallee()
開發者ID:gbarnett,項目名稱:shared-source-cli-2.0,代碼行數:63,代碼來源:reflectionemit.cs

示例5: PrepareAssembly

	//==== Helpers ===================================================

	// Prepare an assembly
	private void PrepareAssembly( string typeName, bool bSave )
	{
		AssemblyBuilderAccess acc = bSave ? AssemblyBuilderAccess.Save:AssemblyBuilderAccess.Run;
		AssemblyName aName = new AssemblyName();
		aName.Name = typeName;

		appdomain = AppDomain.CurrentDomain;
		assemblybuilder = appdomain.DefineDynamicAssembly( aName, acc );
		modulebuilder = assemblybuilder.DefineDynamicModule( typeName+".dll" );
		typebuilder = modulebuilder.DefineType( typeName, TypeAttributes.Public );
	}
開發者ID:jjenki11,項目名稱:blaze-chem-rendering,代碼行數:14,代碼來源:TestEmit.cs


注:本文中的AppDomain.DefineDynamicAssembly方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。