C#类型转换:显式转换存在但引发转换错误?

我了解到HashSet实现了IEnumerable接口。 因此,可以将HashSet对象隐式转换为IEnumerable

 HashSet foo = new HashSet(); IEnumerable foo2 = foo; // Implicit cast, everything fine. 

这也适用于嵌套generics类型:

 HashSet<HashSet> dong = new HashSet<HashSet>(); IEnumerable<IEnumerable> dong2 = dong; // Implicit cast, everything fine. 

至少这就是我的想法。 但如果我创建一个Dictionary ,我会遇到一个问题:

 IDictionary<T, HashSet> bar = new Dictionary<T, HashSet>(); IDictionary<T, IEnumerable> bar2 = bar; // compile error 

最后一行给出了以下编译错误(Visual Studio 2015):

无法隐式转换类型

System.Collections.Generic.IDictionary<T, System.Collections.Generic.HashSet> to System.Collections.Generic.IDictionary<T, System.Collections.Generic.IEnumerable>

存在显式转换(您是否错过了演员?)

但如果我通过写作进行演员表演

 IDictionary<T, IEnumerable> bar2 = (IDictionary<T, IEnumerable>) bar; 

然后我在运行时获得了无效的强制转换exception。

两个问题:

  • 我该如何解决这个问题? 是迭代密钥并逐步构建新字典的唯一方法吗?
  • 为什么我首先会遇到这个问题,即使HashSet实现了IEnumerable接口?

它不起作用的原因是IDictionary值不是共变体 (并且也是关键,出于同样的原因)。 如果允许,则此代码将编译,但必须导致exception:

 IDictionary> foo = new Dictionary>(); IDictionary> bar = foo; foo.Add(key, new List()); 

您认为添加List会起作用,因为它会编译,因为值类型应该是IEnumerable 。 但是,它不能成功,因为实际值类型是HashSet

所以,是的:唯一的方法是创建一个新的字典。

 var bar = foo.ToDictionary(x => x.Key, x => x.Value.AsEnumerable()); 

我该如何解决这个问题? 是迭代密钥并逐步构建新字典的唯一方法吗?

它可能不是最优雅的解决方案,但它有效:

 IDictionary> bar = new Dictionary>(); IDictionary> bar2 = bar.ToDictionary(x => x.Key, y => (IEnumerable)y.Value); 

Dictionary强制转换不起作用的原因是因为IEnumerable是共变量,请注意声明中的

 public interface IEnumerable : IEnumerable 

IDictionary不是。

 public interface IDictionary : ICollection>, IEnumerable>, IEnumerable 

你可以在这里阅读更多相关信息: https : //msdn.microsoft.com/en-us/library/dd799517(v = vs1010).aspx