以下内容无法在线编译 fm.AddFoo(new StringFoo()); 带有错误消息:
参数 1:无法从 'ClassLibrary2.StringFoo' 转换为 'ClassLibrary2.IFoo'
这对我来说似乎是合乎逻辑的,因为字符串继承自对象。
public interface IFoo<T>
{
void Handle(T value);
}
public class StringFoo : IFoo<string>
{
public void Handle(string value)
{ }
}
public class ObjectFoo : IFoo<object>
{
public void Handle(object value)
{ }
}
public class FooManager
{
private readonly List<IFoo<object>> _foos;
public FooManager()
{
_foos = new List<IFoo<object>>();
}
public void AddFoo(IFoo<object> foo)
{
_foos.Add(foo);
}
}
public class Bad
{
public Bad()
{
var fm = new FooManager();
fm.AddFoo(new StringFoo()); \\ This does not compile
}
}
谢谢