问题描述
我创建了以下代码:
public class AddonsModule : Ninject.Modules.NinjectModule
{
public override void Load()
{
this.Bind(b => b.FromAssembliesMatching("*")
.SelectAllClasses()
.InheritedFrom(typeof(UIExtensibility.AbstractAddon))
.BindWith(new AddonBindingGenerator())
);
}
private class AddonBindingGenerator : IBindingGenerator
{
public System.Collections.Generic.IEnumerable<Ninject.Syntax.IBindingWhenInNamedWithOrOnSyntax<object>> CreateBindings(System.Type type, Ninject.Syntax.IBindingRoot bindingRoot)
{
if (type.IsInterface || type.IsAbstract)
yield break;
yield return bindingRoot.Bind(type).ToProvider(typeof(UIExtensibility.AbstractAddon));
}
}
private class AddonProvider : IProvider<UIExtensibility.AbstractAddon>
{
public object Create(IContext context)
{
return null;
}
public Type Type
{
get { throw new NotImplementedException(); }
}
}
}
AddonProvider似乎可以避免.永远不会执行.
AddonProvider seems be avoided. This is never performed.
当我表演时:kernel.GetAll<UIExtensibility.AbstractAddon>()
,AddonProvider.Create
方法从不执行.
When I perform:kernel.GetAll<UIExtensibility.AbstractAddon>()
, AddonProvider.Create
method is never performed.
你能告诉我怎么了吗?非常感谢您的帮助.
Could you tell me what's wrong?I'll appreciate a lot your help.
谢谢大家.
推荐答案
您正在将从typeof(UIExtensibility.AbstractAddon)
继承的特定类型绑定到提供程序.例如,可能有一个class Foo : UIExtensibility.AbstractAddon
.现在,您的约定绑定将转换为:
You're binding a specific type which inherits from typeof(UIExtensibility.AbstractAddon)
to a provider. For example, there could be a class Foo : UIExtensibility.AbstractAddon
.Now your convention binding translates to this:
Bind<Foo>().ToProvider<AddonProvider>();
现在,kernel.GetAll<UIExtensibility.AbstractAddon>()
现在正在寻找像这样的绑定:
Now, kernel.GetAll<UIExtensibility.AbstractAddon>()
however is looking for bindings made like:
Bind<UIExtensibility.AbstractAddon>().To...
修复
所以您需要做的就是更改行
Fix It
So what you need to do is change the line
bindingRoot.Bind(type).ToProvider(new AddonProvider());
收件人:
bindingRoot.Bind(typeof(UIExtensibility.AbstractAddon)).ToProvider<AddonProvider>();
此外
- 您所在的行
object f = bindingRoot.Bind(type).ToProvider(new AddonProvider());
从不返回绑定(object f
). -
UIExtensibility.AbstractAddon
是否实现IProvider
? - you're line
object f = bindingRoot.Bind(type).ToProvider(new AddonProvider());
is never returning the binding (object f
). - does
UIExtensibility.AbstractAddon
implementIProvider
?
Furthermore
这篇关于NInject IBindingGenerator和ToProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!