当前位置: 首页>>代码示例>>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;未经允许,请勿转载。