动态创建和加载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");
}
}
- 动态创建和加载assembly
- Assembly动态加载DLL
- 反射动态加载程序集Assembly.Load和Assembley.LoadFrom
- 动态加载Assembly应用程序(转)
- C#动态加载/卸载Assembly的解决方案
- 动态加载程序集Assembly.Load
- C#动态加载/卸载Assembly的解决方案
- 创建新AppMain 加载Assembly,执行Interface
- Qt plugin 的创建和动态加载
- 利用反射动态加载Assembly动态执行类型方法
- 04 QT创建和加载动态库-②显示加载
- C#中可以通过Assembly来动态加载DLL
- C#动态加载/卸载Assembly的解决方案2
- AppDomain与Assembly的动态加载与卸载
- c#反射中Assembly类动态加载程序集
- 在.Net framework中动态加载Assembly的loadFromRemoteSources配置
- 应用程序配置和动态加载2----创建程序集
- 动态创建IFRAME加载方法和内存释放
- 入住CSDN测试!
- int ,long ,float,char
- 很久
- Configuring SNMP Support
- Android程序的构建块
- 动态创建和加载assembly
- C语言变量的存储类别
- Domino NSD日志诊断/分析
- jsp+servlet+java 实现统计在线人数,在线用户
- JAVA学习【知】IO(二)
- 疯了
- 宏定义小小技巧
- http://blog.csdn.net/pongba/archive/2008/07/08/2625115.aspx
- SharePoint 提供中文SDK了