简体   繁体   English

使用基于类名的反射从其他.dll实例化一个类?

[英]Instantiating a class from different .dll using reflection based on the class name?

Is there any way to instantiate a class based it name from different project/.dll without using dll = Assembly.LoadFile(@"c:\\Test.dll"); 有没有办法在不使用dll = Assembly.LoadFile(@"c:\\Test.dll");情况下从不同的project / .dll实例化基于其名称的类dll = Assembly.LoadFile(@"c:\\Test.dll"); and do dll.CreateInstance(className); 并执行dll.CreateInstance(className);

I want to be able to load different class which implementing a common Interface in my application without need recompiling (something like a plugin). 我希望能够加载在我的应用程序中实现通用接口的其他类,而无需重新编译(类似于插件)。 The dll is in the application path. dll在应用程序路径中。

您需要以某种方式知道要从哪个程序集加载它...然后您可以使用Type.GetType(name) ,其中您传递的名称也要符合程序集名称。

I don't think there is any way to find a Dll based on only a class name or an interface a class implements, except for just finding and opening each Dll in the directory. 我认为没有任何方法可以仅基于类名或类实现的接口来查找Dll,只是查找并打开目录中的每个Dll。 You could use the ReflectionOnlyLoad* methods to reduce some overhead while finding the correct Dll. 您可以使用ReflectionOnlyLoad*方法来减少查找正确Dll时的开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM