配置Ninject返回结果列表我正在使用Ninject来解决依赖关系,它一直游到现在。在这个例子中,我实际上需要一个基于App.config中存储的数据初始化的对象列表。然而,Ninject一直返回一个空列表。下面的代码片段是我尝试过的示例。我已经为某些上下文包含了类层次结构的构造函数。publicServiceSchedulerBuilder(IListtextExportServices){_textExportService=textExportServices;}publicTextExportService(IHotFolderManagerhotFolder){_hotFolder=hotFolder;}publicHotFolderManager(stringbaseFolderPath,stringdefaultModSearchPattern){//一些IO}//在N...Bind().ToMethod(ctx=>{varservices=newList();foreach(vardeviceinGetDevicesByEnvironment()){varservice=ctx.Kernel.Get(newConstructorArgument("hotFolder",ctx.Kernel.Get(newConstructorArgument("baseFolderPath",device.Path),newConstructorArgument("defaultFileSearchPattern","*.jmf"))));services.Add(service);}返回服务;});我怀疑这一切都源于我没有为ITextExportService本身注册显式绑定。但是由于实现将取决于来自App.config的数据,所以我无法弄清楚如何注册它而不是每次我请求该类型的列表时都接收相同的实例。相关:Kernel.Get和构造函数注入之间Ninject的不同行为->在执行kernel.Get>和解析ctor的IList参数时,ninject的行为不匹配。所以就是这样:ninject的多重注入功能在这里具有优先权。每当ninject遇到对IEnumerable、IList或T[](但AFAIR不是ICollection)的请求时,它会将其转换为解析T的所有绑定(无条件或匹配条件)的请求。尝试以下操作:publicinterfaceINoMultiBindingList:IList{}publicclassNoMultiBindingList:List,INoMultiBindingList{}with:sbbpublicServiceSchedulerBuilder(INoMultiBindingListtextExportServices){textExportService=textExportServices;内容,如果对你有用,需要了解更多C#学习教程,希望大家多多关注——Kernel.Bind>().ToMethod(ctx=>{varservices=newNoMultiBindingList();foreach(vardeviceinGetDevicesByEnvironment()){varservice=ctx.Kernel.Get(newConstructorArgument("hotFolder",ctx.Kernel.Get(newConstructorArgument("baseFolderPath",device.Path),newConstructorArgument("defaultFileSearchPattern","*.jmf"))));services.Add(service);}返回服务;});本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: