我正在尝试围绕界面进行思考。我一直在尝试这样的事情:
public interface IFoo
{
ICollection<IBar> Bars { get; set; }
//some other properties
}
public interface IBar
{
//some properties
}
//assume Bar is implemented and extends IBar. Some instaces of Bar are created
Bar[] MyBars = {bar1, bar2};
Foo MyFoo = new Foo();
MyFoo.Bars=MyBars.ToList(); //This causes an error saying Collection<Bar> cannot be
//assigned to ICollection<IBar>.
我完全不知道如何正确地做到这一点。填充接口集合的正确方法是什么?
编辑:我实现 Foo 的方式可能是问题的一部分。
public class Foo : IFoo
{
public ICollection<IBar> Bars { get; set; }
}