Dependency injection 错误的类型

Dependency injection 错误的类型,dependency-injection,ninject,Dependency Injection,Ninject,我有一个类,它的属性类型为ICollection,我正试图用类型为observedcollection的实例注入该类。如果我通过GetNinject获得一个实例,它会为我提供正确的类型,但是如果我让它将其注入到一个类中,它会创建一个列表。有人能解释一下发生了什么事吗 using Ninject; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; na

我有一个类,它的属性类型为
ICollection
,我正试图用类型为
observedcollection
的实例注入该类。如果我通过
Get
Ninject获得一个实例,它会为我提供正确的类型,但是如果我让它将其注入到一个类中,它会创建一个
列表
。有人能解释一下发生了什么事吗

using Ninject;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace NinjectTest
{
    class Program
    {
        static void Main(string[] args)
        {
            IKernel kernel = new StandardKernel();
            kernel.Bind<MyClass>().ToSelf();
            kernel.Bind<ICollection<object>>().To(typeof(ObservableCollection<object>));

            ICollection<object> foo = kernel.Get<ICollection<object>>();
            ICollection<object> bar = kernel.Get<MyClass>().Collection;

            Debug.Assert(foo is ObservableCollection<object>); // ok
            Debug.Assert(bar is ObservableCollection<object>); // fails
        }
    }

    public class MyClass
    {
        [Inject] public ICollection<object> Collection { get; set; }
    }
}
使用Ninject;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用系统诊断;
命名空间测试
{
班级计划
{
静态void Main(字符串[]参数)
{
IKernel kernel=新的标准内核();
kernel.Bind().ToSelf();
Bind().To(typeof(ObservableCollection));
ICollection foo=kernel.Get();
ICollection bar=kernel.Get().Collection;
Assert(foo是ObservableCollection);//确定
Assert(bar是ObservableCollection);//失败
}
}
公共类MyClass
{
[Inject]公共ICollection集合{get;set;}
}
}
这是根据“”功能进行的。 当您尝试注入一个数组
[]
、一个
IEnumerable
、一个
ICollection
或一个
IList
时,它总是被解释为
GetAll
,因此它将为
T
创建每个绑定的实例,并将其作为您请求的“集合”类型返回

到目前为止,我所做的是创建特定的接口,如:

public interface IFooCollection : ICollection<Foo>

好的,那当然可以解释。我没有意识到在这些情况下会使用多重注入,尽管现在我想这是有道理的。我的原始代码现在已修复,谢谢您的解释。
public interface IObservableCollection<T> : ICollection<T>, INotifyCollectionChanged