【发布时间】:2018-08-29 09:41:11
【问题描述】:
我有一个字典对象,例如:-
var gridContent = new Dictionary<T, KeyValuePair<int, bool>>();
现在我想把它输入:-
Dictionary<SomeClass, KeyValuePair<int, bool>>();
有什么办法吗?
【问题讨论】:
-
你能在上下文中展示一个更完整的例子来说明你正在尝试做什么吗?
-
如果它是
Dictionary<SomeClass, KeyValuePair<int, bool>>,那么您可以像进行任何其他演员一样投射它。 -
不幸的是,你不能简单地投射这个:
error CS0030: Cannot convert type 'System.Collections.Generic.Dictionary<SomeClassB, System.Collections.Generic. KeyValuePair<int, bool>>' to 'System.Collections.Generic.Dictionary<SomeClass, System.Collections.Generic.KeyValuePair<i nt, bool>>' -
为什么要这样做?你在一个泛型类中,为什么它必须知道
SomeClass的类型呢? -
泛型成员的意义在于它适用于每一种类型,而不仅仅是特定的类型。因此,如果您只有以
SomeClass-elements 作为键的字典,为什么不使用Dictionary<SomeClass, ...>?请提供一个更有意义的示例来说明您想要实现的目标,最好以Minimal, Complete, and Verifiable example 的形式
标签: c# casting type-conversion explicit-conversion