动态创建和加载assembly

来源:互联网 发布:淘宝天梭手表是正品吗 编辑:程序博客网 时间:2024/05/22 06:20

Reference: http://msdn.microsoft.com/zh-cn/library/7hcs6az6(VS.80).aspx

 

using System;

using System.IO;

using System.Reflection;

using System.Reflection.Emit;

 

class Test

{

public static void Main()

{

      AppDomain currentDomain = AppDomain.CurrentDomain;

     

      //Current Domain

 

      Console.WriteLine("Current Domain");

      //InstantiateMyType(currentDomain);   // Failed!

      currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolver);

      InstantiateMyType(currentDomain);   // OK!

      Console.WriteLine();

 

      //Second Domain

      Console.WriteLine("Second Domain");

      System.Security.Policy.Evidence securityInfo = new System.Security.Policy.Evidence(currentDomain.Evidence);

      string appBasePath = currentDomain.BaseDirectory;

 

      AppDomain secondDomain = AppDomain.CreateDomain("Test dynamic create and load assembly",securityInfo,appBasePath,null,false);

      secondDomain.AssemblyResolve += new ResolveEventHandler(MyResolver);

      InstantiateMyType(secondDomain);

      AppDomain.Unload(secondDomain);

      Console.Read();

 

}

  

static void InstantiateMyType(AppDomain domain)

{

      try

      {

          // You must supply a valid fully qualified assembly name here.

          object obj = domain.CreateInstance("Assembly text name, Version, Culture, PublicKeyToken", "MyType").Unwrap();

          Console.WriteLine("Dynamic object created!");

      }

      catch (Exception e)

      {

          Console.WriteLine(e.Message);

      }

}

  

// Loads the content of a file to a byte array.

static byte[] loadFile(string filename)

{

      FileStream fs = new FileStream(filename, FileMode.Open);

      byte[] buffer = new byte[(int) fs.Length];

      fs.Read(buffer, 0, buffer.Length);

      fs.Close();

  

      return buffer;

}  

 

public static Assembly MyResolver(object sender, ResolveEventArgs args)

{

      AppDomain domain = (AppDomain) sender;

 

      // Once the files are generated, this call is

      // actually no longer necessary.

      EmitAssembly(domain);

     

      byte[] rawAssembly = loadFile("MyAssembly.dll");

      byte[] rawSymbolStore = loadFile("MyAssembly.pdb");

      Assembly assembly = domain.Load(rawAssembly, rawSymbolStore);

 

      return assembly;

}

  

// Creates a dynamic assembly with symbol information

// and saves them to temp.dll and temp.pdb

static void EmitAssembly(AppDomain domain)

{

      AssemblyName assemblyName = new AssemblyName();

      assemblyName.Name = "MyAssembly";

 

      AssemblyBuilder assemblyBuilder = domain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Save);

      ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MyModule", "MyAssembly.dll", true);

      TypeBuilder typeBuilder = moduleBuilder.DefineType("MyType", TypeAttributes.Public | TypeAttributes.Serializable);

 

      ConstructorBuilder constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, null);

      ILGenerator ilGenerator = constructorBuilder.GetILGenerator();

      ilGenerator.EmitWriteLine("MyType instantiated!");

      ilGenerator.Emit(OpCodes.Ret);

 

      System.Type myType = typeBuilder.CreateType();

     

      assemblyBuilder.Save("MyAssembly.dll");

}

}