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