给定以下类和接口:
public class Test<T>
{
}
public interface ITesting<T>
{
Test<T> LoadTest();
}
我可以通过以下方式添加一个类:
public class TestManager : ITesting<object>
{
#region ITesting
public Test<object> LoadTest()
{
return new Test<object>();
}
#endregion
}
这段代码运行正常,没有出现任何错误。如果我将该类替换为以下内容:
public class TestDerived : Test<object>
{
}
public class TestDerivedManager : ITesting<object>
{
#region ITesting
public TestDerived LoadTest()
{
return new TestDerived();
}
#endregion
}
我现在遇到了以下错误:
Error 'TestDerivedManager'没有实现接口成员 'ITesting.LoadTest()'。因为它没有匹配的返回类型 'Test',所以'TestDerivedManager.LoadTest()'无法实现'ITesting.LoadTest()'。但是对我来说,这应该是可以工作的,因为
TestDerived
是Test<object>
。显然我理解错了什么,有没有可能有人指出为什么这是不正确的?也可能指出我可能怎样才能纠正它吗?
interface I { Animal M(); } class C : I { public Giraffe M() { return new Giraffe(); } }
,你将会得到相同的错误。 - Eric Lippert