Ninject:是否可以加载声明为内部的模块 [英] Ninject: Can modules be loaded that are declared as internal
问题描述
是否可以配置Ninject加载已声明为internal
的模块?
Is it at all possible to configure Ninject to load modules that have been declared as internal
?
我尝试为Ninject程序集配置InternalVisibleTo
,但这无济于事.
I have tried configuring InternalVisibleTo
for the Ninject assembly, but this does not help.
我当然可以将模块制作为public
,但实际上它们应该是internal
.
I can of course make the modules public
, but really they should be internal
.
推荐答案
内部KernalBase.Load(IEnumerable<Assembly assemblies)
使用GetExportedTypes()
仅返回公共类型.
Internally KernalBase.Load(IEnumerable<Assembly assemblies)
uses the GetExportedTypes()
which only returns public types.
但是,您可以编写自己的"NinjectModule扫描仪".
However, you could write your own "NinjectModule scanner".
public static class NinjectModuleScanner
{
public static IEnumerable<INinjectModule>
GetNinjectModules(IEnumerable<Assembly assemblies)
{
return assemblies.SelectMany(assembly => assembly.GetNinjectModules());
}
}
public static class AssemblyExtensions
{
public static IEnumerable<INinjectModule>
GetNinjectModules(this Assembly assembly)
{
return assembly.GetTypes()
.Where(IsLoadableModule)
.Select(type => Activator.CreateInstance(type) as INinjectModule);
}
private static bool IsLoadableModule(Type type)
{
return typeof(INinjectModule).IsAssignableFrom(type)
&& !type.IsAbstract
&& !type.IsInterface
&& type.GetConstructor(Type.EmptyTypes) != null;
}
}
然后您可以执行以下操作.
Then you could do the following.
var modules = NinjectModuleScanner.GetNinjectModules(assemblies).ToArray();
var kernel = new StandardKernel();
此解决方案尚未经过测试.
This solution has not been tested though.
这篇关于Ninject:是否可以加载声明为内部的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!