【发布时间】:2013-10-31 01:56:39
【问题描述】:
我有如下界面:
public interface IObject{
double x {get;}
double y {get;}
List<IObject> List{get; set;}
}
还有这个类
public class Holder<T> where T : IObject {
private T myItem;
public void ChangeItemList(T item){
myItem.List = item.List;
}
但是编译器不喜欢 ChangeItemList 方法并且在这一行:
myItem.List = item.List;
给我这个错误:
Cannot convert source type 'List<T>' to target type 'List<IObject>'
为什么我不能这样做?对于这种情况有什么好的解决方案? 谢谢你
【问题讨论】:
-
您不可能拥有该接口,因为接口不能包含字段。此代码无法编译。
-
您的界面中似乎有成员字段。这是不允许的。双x;和双 y;不能在接口中声明。
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
-
对不起,它们应该是属性。我编辑了我的代码。
-
为什么不:public void ChangeItemList(IObject item)?