C# 如何在运行时加载程序集并创建类实例?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1803540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to load Assembly at runtime and create class instance?
提问by Pankaj
I have a assembly. In this assembly I have a class and interface. I need to load this assembly at runtime and want to create an object of the class and also want to use the interface.
我有一个大会。在这个程序集中,我有一个类和接口。我需要在运行时加载这个程序集,并且想要创建类的对象并且还想要使用接口。
Assembly MyDALL = Assembly.Load("DALL"); // DALL is name of my dll
Type MyLoadClass = MyDALL.GetType("DALL.LoadClass"); // LoadClass is my class
object obj = Activator.CreateInstance(MyLoadClass);
This is my code. How could it be improved?
这是我的代码。如何改进?
采纳答案by Mehdi Golchin
If your assembly is in GAC or bin use the assembly name at the end of type name instead of Assembly.Load()
.
如果您的程序集在 GAC 或 bin 中,请在类型名称的末尾使用程序集名称而不是Assembly.Load()
.
object obj = Activator.CreateInstance(Type.GetType("DALL.LoadClass, DALL", true));
回答by Sasikumar D.R.
You should Use Dynamic Method with for Improving. its faster than reflection..
您应该使用动态方法进行改进。它比反射更快..
Here is a sample code for creating Object using Dynamic Method..
这是使用动态方法创建对象的示例代码..
public class ObjectCreateMethod
{
delegate object MethodInvoker();
MethodInvoker methodHandler = null;
public ObjectCreateMethod(Type type)
{
CreateMethod(type.GetConstructor(Type.EmptyTypes));
}
public ObjectCreateMethod(ConstructorInfo target)
{
CreateMethod(target);
}
void CreateMethod(ConstructorInfo target)
{
DynamicMethod dynamic = new DynamicMethod(string.Empty,
typeof(object),
new Type[0],
target.DeclaringType);
ILGenerator il = dynamic.GetILGenerator();
il.DeclareLocal(target.DeclaringType);
il.Emit(OpCodes.Newobj, target);
il.Emit(OpCodes.Stloc_0);
il.Emit(OpCodes.Ldloc_0);
il.Emit(OpCodes.Ret);
methodHandler = (MethodInvoker)dynamic.CreateDelegate(typeof(MethodInvoker));
}
public object CreateInstance()
{
return methodHandler();
}
}
//Use Above class for Object Creation.
ObjectCreateMethod inv = new ObjectCreateMethod(type); //Specify Type
Object obj= inv.CreateInstance();
This method takes only 1/10th time needed by Activator.
这种方法只需要 Activator 所需时间的 1/10。
Check out http://www.ozcandegirmenci.com/post/2008/02/Create-object-instances-Faster-than-Reflection.aspx
查看http://www.ozcandegirmenci.com/post/2008/02/Create-object-instances-Faster-than-Reflection.aspx
回答by reggaeguitar
Check out http://www.youtube.com/watch?v=x-KK7bmo1AMTo modify his code to load multiple assemblies use
查看http://www.youtube.com/watch?v=x-KK7bmo1AM要修改他的代码以加载多个程序集,请使用
static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
string assemblyName = args.Name.Split(',').First();
using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("YourNamespace." + assemblyName + ".dll"))
{
byte[] assemblyData = new byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return Assembly.Load(assemblyData);
}
}
在你的主要方法中放
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
请务必将您的程序集添加到您的项目并将构建操作属性更改为“嵌入式资源”。