【发布时间】:2011-12-06 14:45:17
【问题描述】:
我需要一些建议/帮助,我再也看不到树上的木头了。
这是一系列使用泛型实现一些接口的简单类。
然后我尝试转换具体类型,例如:
MyGenericObject<SomeObject> _obj;
IMyGenericObject<ISomeObject> _genObj = (IMyGenericObject<ISomeObject>)_obj;
// 无效转换
我读过一些关于协变和逆变的文章,但不太清楚为什么这是不可能的,或者如何绕过它?
所以,在这个例子中:
public interface IMyObject<in T> where T : IBaseObject
{
T Activity { get; set; }
}
没用……
....因为,您无法获取和设置 Activity 属性。
在这个例子中,我需要做:
public interface IMyObject<out T> where T : IBaseObject
{
T Activity { get; }
}
希望对某人有所帮助,感谢大家的帮助!
【问题讨论】:
-
请显示你的接口定义