我正在尝试返回IEnumerable<IMyInterface>
。我有一个类,MyClass:IMyInterface
我从一个函数返回。
IEnumerable<IMyInterface> test() {
tmpList = new List<MyClass>();
tmp1 = new MyClass();
tmp2 = new MyClass();
tmpList.Add(tmp1);
tmpList.Add(tmp2);
return tmpList;
}
编译器不允许,这对我来说似乎很奇怪,因为 MyClass:MyInterface。编译器给出错误'cannot implicitly convert type System.Collections.Generic.IEnumerable<MyClass> to System.Collections.Generic.IEnumerable<IMyInterface. An explicit conversion exists. Are you missing a cast?'
我无法(IEnumerable<IMyInterface>)tmp
在运行时执行没有强制转换异常的返回。我错过了什么?我希望返回接口的 IEnumerable 应该可以正常工作。