小编jow*_*ggs的帖子

使用NInject时,在注入a a ctor arg时解析IEnumerable <T>会失败,但在执行Get <IEnumerable <T >>()时则不会

当我为IEnumerable设置绑定时使用NInject时,如果我直接请求IEnumerable,它将起作用,但如果另一个绑定对象需要IEnumerable则不行.这是设计的吗?

class Program {
    static void Main(string[] args){
        var k = new StandardKernel();
        k.Bind<IEnumerable<int>>().ToMethod(GetInts);
        k.Bind<IFoo>().To<Foo>(); //Has an IEnumberable<int> constructor arg
        var works = k.Get<IEnumerable<int>>(); //returns the array of ints
        var tst = k.Get<IFoo>(); //Empty integer array is passed in by ninject???
        tst.Get(); //returns an empty integer array????
        return;
    }

    public static int[] GetInts(IContext ctx){
        return new int[] {1,2,3,4,5};
    }
}


public interface IFoo{
    IEnumerable<int> Get();
}


public class Foo : IFoo{
    private int[] _vals;

    public Foo(IEnumerable<int> vals){
        _vals = vals.ToArray(); …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection ninject

5
推荐指数
1
解决办法
983
查看次数

标签 统计

c# ×1

dependency-injection ×1

ninject ×1