C# Ninject工厂没有为我处理约定
我正在尝试使用位于此处的绑定方法,但运气不佳 请记住,我不是想这样做: 相反,我试图使用约定GetMercedes()来表示 我基本上是在尝试实现这一点:使用上述示例中指定的约定C# Ninject工厂没有为我处理约定,c#,ninject,C#,Ninject,我正在尝试使用位于此处的绑定方法,但运气不佳 请记住,我不是想这样做: 相反,我试图使用约定GetMercedes()来表示 我基本上是在尝试实现这一点:使用上述示例中指定的约定 using Ninject; using Ninject.Extensions.Factory; using Ninject.Modules; using Ninject.Parameters; using System; using System.Collections.Generic; using System.
using Ninject;
using Ninject.Extensions.Factory;
using Ninject.Modules;
using Ninject.Parameters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Test.NinjectFactory
{
public class Program
{
public static void Main()
{
using (var kernel = new StandardKernel(new CarModule()))
{
var factory = kernel.Get<ICarFactory>();
var mercedes =factory.GetMercedes();
int i = 1;
}
}
public interface ICar
{
void Drive();
}
public class Mercedes : ICar
{
readonly ICarFactory carFactory;
public Mercedes(ICarFactory carFactory)
{
this.carFactory = carFactory;
}
public void Drive()
{
var Mercedes = this.carFactory.GetMercedes();
}
}
public interface ICarFactory
{
ICar GetMercedes();
}
public class CarModule : NinjectModule
{
public override void Load()
{
//https://github.com/ninject/ninject.extensions.factory/wiki/Factory-interface%3A-Referencing-Named-Bindings
Kernel.Bind<ICarFactory>().ToFactory();
Bind<ICar>().To<Mercedes>().NamedLikeFactoryMethod<ICarFactory>(x => x.GetMercedes());//doesnt work for me
}
}
}
}
使用Ninject;
使用Ninject.Extensions.Factory;
使用Ninject.Modules;
使用Ninject.参数;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
运用系统反思;
使用系统文本;
使用System.Threading.Tasks;
命名空间Test.ninject工厂
{
公共课程
{
公共静态void Main()
{
使用(var-kernel=new-StandardKernel(new-CarModule()))
{
var factory=kernel.Get();
var mercedes=factory.GetMercedes();
int i=1;
}
}
公共接口ICar
{
无效驱动();
}
公共级梅赛德斯:ICar
{
只读ICarFactory汽车工厂;
公共梅赛德斯(ICarFactory carFactory)
{
this.carFactory=carFactory;
}
公共车道()
{
var Mercedes=this.carFactory.GetMercedes();
}
}
公共接口ICarFactory
{
ICar GetMercedes();
}
公共类CarModule:ninject模块
{
公共覆盖无效负载()
{
//https://github.com/ninject/ninject.extensions.factory/wiki/Factory-interface%3A-引用命名绑定
Kernel.Bind().ToFactory();
Bind().To().NamedLikeFactoryMethod(x=>x.GetMercedes());//对我不起作用
}
}
}
}
我将此作为答案发布,因为这很可能是原因
工厂扩展使用带前缀的Get
方法作为标准。如果使用前缀Get
调用任何factory方法并使用NamedLikeFactoryMethod
,您将遇到问题。例如,GetFord
,GetMercedes
,GetNissan
。您会注意到,在您提供的链接示例中,函数名为CreateMercedes
将函数名更改为CreateMercedes
或任何不以Get
开头的函数名,应该可以了。我在这里找到了答案:
似乎您需要一个函数来处理绑定
public class BaseTypeBindingGenerator<InterfaceType> : IBindingGenerator
{
public IEnumerable<IBindingWhenInNamedWithOrOnSyntax<object>> CreateBindings(Type type, IBindingRoot bindingRoot)
{
if (type != null && !type.IsAbstract && type.IsClass && typeof(InterfaceType).IsAssignableFrom(type))
{
string.Format("Binds '{0}' to '{1}' as '{2}", type, type.Name, typeof(InterfaceType)).Dump();
yield return bindingRoot.Bind(typeof(InterfaceType))
.To(type)
.Named(type.Name) as IBindingWhenInNamedWithOrOnSyntax<object>;
}
}
}
公共类BaseTypeBindingGenerator:IBindingGenerator
{
公共IEnumerable CreateBindings(类型类型,IBindingRoot bindingRoot)
{
if(type!=null&&!type.isastract&&type.IsClass&&typeof(InterfaceType).IsAssignableFrom(type))
{
Format(“将“{0}”绑定到“{1}”作为“{2}”,type,type.Name,typeof(InterfaceType)).Dump();
返回bindingRoot.Bind(typeof(InterfaceType))
.至(类型)
.命名(type.Name)为IBinding,当使用OronSyntax命名时;
}
}
}
目标是使用这样的约定:这样建议可能不是我想要的。感谢您花时间回复。