我是 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();
}
}
}
}
有什么方法或解决方案可以满足我的需求吗?
谢谢!安娜