【发布时间】:2014-05-14 15:06:34
【问题描述】:
我需要实现一个集合(例如,SpecCollection),它应该包含KEY 和VALUE。键应该是复合键。请看简短的实现:
public class CompositeKey<TId, TName>
{
private TId _id;
private TName _name;
public TId Id { get; set; }
public TName Name { get; set; }
}
我应该只通过以下方式初始化我的集合:
SpecialCollection<CompositeKey<string, int>, DateTime> e = new
SpecialCollection<CompositeKey<string, int>, DateTime>();
或者像这样:
SpecialCollection<CompositeKey<DateTime, int>, SomeClass> e = new
SpecialCollection<CompositeKey<DateTime, int>, SomeClass>();
如果可能,请告诉我如何声明我的收藏?
我找到了以下方法:
public class SpecialCollection<TId, TName, TValue>
: Dictionary<CompositeKey<TId, TName>, TValue>
{
private Dictionary<CompositeKey<TId, TName>, TValue> _baseDictionary = new
Dictionary<CompositeKey<TId, TName>, TValue>();
}
但在这种情况下,我只能像这样初始化集合对象:
SpecialCollection<DateTime, int, string> col =
new SpecialCollection<DateTime, int, string>();
您对如何声明集合有任何想法吗?
谢谢!
【问题讨论】:
-
我也是?你怎么了?你想要什么?
-
OP 正在寻找一种在不指定类型的情况下使用
CompositeKey<>作为SpecialCollection<>的类型参数的方法。 -
我需要实现一个只能像这样初始化的集合:
SpecialCollection<CompositeKey<AnyType, AnyType>, AnyType> e = new SpecialCollection<CompositeKey<AnyType, AnyType>, AnyType>();集合的键应该是集合的键应该是CompositeKey -
鲍勃森,你说的对!
标签: c# generics collections