【发布时间】:2012-02-01 23:10:04
【问题描述】:
我想从解决方案文件夹中的多个程序集检索实现接口的实例化类的枚举。
我有以下文件夹结构(如果这有意义的话):
Solution
-SolutionFolder
- Project1
- class implementing interface I would like to find
- other classes
- Project2
- class implementing interface I would like to find
- other classes
-MainProject
- classes where my code is running in which I would like to retrieve the list of classes
因此,如果正在实现的接口是ISettings,那么我想要一个IEnumerable<ISettings> 引用该接口的实例化对象。
到目前为止,我已经使用反射从已知的类名中检索实现接口的类:
IEnumerable<ISettings> configuration =
(from t in Assembly.GetAssembly(typeof(CLASSNAME-THAT-IMPLEMENTs-INTERFACE-HERE)).GetTypes()
where t.GetInterfaces().Contains(typeof(ISettings)) && t.GetConstructor(Type.EmptyTypes) != null
select (ISettings)Activator.CreateInstance(t)).ToList();
但这是一个单独的程序集,我实际上并不知道类名。
这可以使用反射来实现还是需要更多的东西?
【问题讨论】:
标签: c# reflection assemblies