我在C#中的新东西......(.NET 3.5)列表与不同类型的
我想要一个字典持有两种不同类型的对象,该类型的一个是通用的。在遍历列表时,我会调用add和clone之类的方法。 我有一个基类和子类试了一下....
namespace ConsoleApplication1 {
class Element{
}
class Child1 : Element {
public Child1 Clone() { return clone; }
}
class Child2<T> : Element {
public Child2<T> Clone() { return clone; }
}
class Program {
static void Main(string[] args) {
Dictionary<string, Element> d = new Dictionary<string, Element>();
d.Add("c1", new Child1());
d.Add("c2s", new Child2<string>());
d.Add("c2i", new Child2<int>());
foreach (KeyValuePair<string, Element> kvp in d) {
Element e = kvp.Value.Clone();
}
}
}
}
是否有我需要的方式或解决方案?
谢谢! Anna