【发布时间】:2015-12-24 13:13:30
【问题描述】:
我已经看过一些类似的答案,但我无法让它发挥作用。
我正在尝试使以下内容更易于维护:
var modules = new INinjectModule[]
{
new ServiceModule(),
new ApplicationSettingsModule(),
new SerializerModule(),
new LoggerModule(),
new SqliteModule(),
new SetupModule(),
new CacheModule(),
new AuthenticationModule(),
};
每次我添加一个新的NinjectModule 时,我都需要修改这个数组以包含它。
我希望能够找到从NinjectModule 派生的所有类型并激活它们并将它们全部放入一个集合中。
这是我尝试过的,但我没有得到任何派生自 NinjectModule 的类
var classes = (from domainAssembly in AppDomain.CurrentDomain.GetAssemblies()
from assemblyType in domainAssembly.GetTypes()
where typeof(NinjectModule).IsAssignableFrom(assemblyType)
select assemblyType).ToArray();
请注意,我要查找的类位于不同的程序集中...
【问题讨论】:
-
AppDomain.CurrentDomain.GetAssemblies()仅返回在您调用它时已加载的程序集(请参阅stackoverflow.com/a/10284950/292411) - 您可以检查此时是否包含包含所需类型的程序集
标签: c# reflection ninject