相关疑难解决方法(0)

在C#中:如何声明类型为键的通用字典和该类型的IEnumerable <>作为值?

我想声明一个存储IEnumerable特定类型的类型的字典,并将其确切类型作为键,如下所示:(编辑后跟随johny g的评论)

private IDictionary<Type, IEnumerable<T>> _dataOfType where T: BaseClass; //does not compile!
Run Code Online (Sandbox Code Playgroud)

我想要存储的具体类都是从BaseClass派生的,因此想把它当作约束.编译器抱怨它希望在成员名称后面加一个分号.

如果它可以工作,我希望这将使后来从字典中检索简单如下:

IEnumerable<ConcreteData> concreteData;
_sitesOfType.TryGetValue(typeof(ConcreteType), out concreteData);
Run Code Online (Sandbox Code Playgroud)

如何定义这样的字典?

c# generics dictionary types

26
推荐指数
4
解决办法
5万
查看次数

标签 统计

c# ×1

dictionary ×1

generics ×1

types ×1