我是新来的c#和试图学习通用接口。尝试实现GetComments时出现错误。错误:'cannot convert expression type 'WallGetObject' to return type T'
。 有没有可能做这样的事情?谢谢。在c#中实现通用接口#
interface IInterface
{
T GetComments<T>(int id);
}
public class A: IInterface
{
public T GetComments<T>()
{
WallGetObject item
//... getting item
return item
}
}
public class B : IInterface
{
public T GetComments<T>()
{
TopicFeedObject item
//... getting item
return item
}
}
public class MainClass()
{
var item1 = new A();
var item2 = new B();
var list = new List<IInterface>();
list.Add(item1);
list.Add(item2);
foreach (var i in list)
{
i.GetComments(5);
}
您正在将泛型接口与泛型方法混合使用。你定义它的方式,你有泛型方法的非泛型接口。 –