【发布时间】:2017-06-23 13:57:16
【问题描述】:
我尝试让所有类都使用此方法实现接口:
private static IEnumerable<Type> GetDriverClasses()
{
var type = typeof(IDeviceDriver);
var types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => type.IsAssignableFrom(p) && p.IsClass && !p.IsAbstract);
return types;
}
只要创建了类的实例,它就可以工作。否则它会失败。
如何在无需先创建实例的情况下获取类?
附加信息:
DllSetup:
Core.dll 持有具有上述方法的类
Impl.dll 引用 Core 并持有要查找的类
Test.dll 引用两者并调用方法
这似乎与如何加载程序集有关,因为在 Impl.dll 中创建一个虚拟类的实例也会使其他类可以找到。
【问题讨论】:
-
使用
AppDomain.Load()强制将程序集加载到应用程序域中 -
它与类的实例没有任何关系,但如果库(.dll)已经加载到应用程序域中。
标签: c#