【发布时间】:2012-08-08 04:35:59
【问题描述】:
我的问题有点类似于 Generic List of Generic Interfaces not allowed, any alternative approaches?
如果我有这样的界面
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
}
我不确定这怎么可能。
谢谢, 抢
【问题讨论】:
标签: c# list generics interface generic-list