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


C# AppDomain.DefineDynamicAssembly方法代碼示例

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


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

示例1: Main

//引入命名空間
using System;
using System.Reflection;
using System.Reflection.Emit;

class Test {
   public static void Main() {
      AppDomain currentDomain = AppDomain.CurrentDomain;

      InstantiateMyDynamicType(currentDomain);   // Failed!
      
      currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
      
      InstantiateMyDynamicType(currentDomain);   // OK!
   }

   static void InstantiateMyDynamicType(AppDomain domain) {
      try {
         // You must supply a valid fully qualified assembly name here. 
         domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyDynamicType");
      } catch (Exception e) {
         Console.WriteLine(e.Message);
      }
   }   

   static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args) {
      return DefineDynamicAssembly((AppDomain) sender);
   }
   
   static Assembly DefineDynamicAssembly(AppDomain domain) {
      // Build a dynamic assembly using Reflection Emit API.
   
      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:.NET開發者,項目名稱:System,代碼行數:49,代碼來源:AppDomain.DefineDynamicAssembly

示例2: Main

//引入命名空間
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Security;

class Example
{
    static void Main()
    {
        // Create a CustomAttributeBuilder for the assembly attribute. 
        // 
        // SecurityTransparentAttribute has a parameterless constructor, 
        // which is retrieved by passing an array of empty types for the
        // constructor's parameter types. The CustomAttributeBuilder is 
        // then created by passing the ConstructorInfo and an empty array
        // of objects to represent the parameters.
        //
        ConstructorInfo transparentCtor = 
            typeof(SecurityTransparentAttribute).GetConstructor(
                Type.EmptyTypes);
        CustomAttributeBuilder transparent = new CustomAttributeBuilder(
            transparentCtor,
            new Object[] {} );
      
        // Create a dynamic assembly using the attribute. The attribute is
        // passed as an array with one element.
        AssemblyName aName = new AssemblyName("EmittedAssembly");
        AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly( 
            aName, 
            AssemblyBuilderAccess.Run,
            new CustomAttributeBuilder[] { transparent } );

        ModuleBuilder mb = ab.DefineDynamicModule( aName.Name );
        TypeBuilder tb = mb.DefineType( 
            "MyDynamicType", 
            TypeAttributes.Public );
        tb.CreateType();

        Console.WriteLine("{0}\nAssembly attributes:", ab);
        foreach (Attribute attr in ab.GetCustomAttributes(true))
        {
            Console.WriteLine("\t{0}", attr);
        }
    }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:46,代碼來源:AppDomain.DefineDynamicAssembly

輸出:

EmittedAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
Assembly attributes:
        System.Security.SecurityTransparentAttribute

示例3: Main

//引入命名空間
using System;
using System.Reflection;
using System.Reflection.Emit;

public class MainClass 
{

    public static void Main() 
    {
        AppDomain ad = AppDomain.CurrentDomain;

        AssemblyName an = new AssemblyName();
        an.Name = "DynamicRandomAssembly";
        AssemblyBuilder ab = ad.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);

        ModuleBuilder mb = ab.DefineDynamicModule("RandomModule");

        TypeBuilder tb = mb.DefineType("DynamicRandomClass",TypeAttributes.Public);

        Type returntype = typeof(int);
        Type[] paramstype = new Type[0];
        MethodBuilder methb=tb.DefineMethod("DynamicRandomMethod", MethodAttributes.Public, returntype, paramstype);

        ILGenerator gen = methb.GetILGenerator();
        gen.Emit(OpCodes.Ldc_I4, 1);
        gen.Emit(OpCodes.Ret);

        Type t = tb.CreateType();

        Object o = Activator.CreateInstance(t);
        Object[] aa = new Object[0];
        MethodInfo m = t.GetMethod("DynamicRandomMethod");
        int i = (int) m.Invoke(o, aa);
        Console.WriteLine("Method {0} in Class {1} returned {2}",m, t, i);

    }
}

using System;

public class DynamicRandomClass
{
    public int DynamicRandomMethod()
    {
        return 1;
    }
}
開發者ID:C#程序員,項目名稱:System,代碼行數:48,代碼來源:AppDomain.DefineDynamicAssembly


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