我的问题有点类似于Generic List of Generic Interfaces not allowed, any alternative approaches?
如果我有这样一个接口:
当遍历这个列表时,如何获取Value属性?
public interface IPrimitive
{
}
public interface IPrimitive<T> : IPrimitive
{
T Value { get; }
}
public class Star : IPrimitive<string> //must declare T here
{
public string Value { get { return "foobar"; } }
}
public class Sun : IPrimitive<int>
{
public int Value { get { return 0; } }
}
然后我有一个列表
var myList = new List<IPrimitive>();
myList.Add(new Star());
myList.Add(new Sun());
当遍历这个列表时,如何获取Value属性?
foreach (var item in myList)
{
var value = item.Value; // Value is not defined in IPrimitive so it doesn't know what it is
}
我不确定这是否可能。
谢谢, 罗布