C#类型转换:显式类型转换存在,但会抛出一个转换错误? [英] C# type conversion: Explicit cast exists but throws a conversion error?

查看:277
本文介绍了C#类型转换:显式类型转换存在,但会抛出一个转换错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解到, HashSet的实施的IEnumerable 接口。因此,有可能隐含蒙上了 HashSet的对象为的IEnumerable

I learned that HashSet implements the IEnumerable interface. Thus, it is possible to implicitly cast a HashSet object into IEnumerable:

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

这适用于嵌套泛型类型,也:

This works for nested generic types, too:

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

目前至少这是我的想法。但是,如果我做一个词典,我碰到一个问题:

At least that's what I thought. But if I make a Dictionary, I run into a problem:

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



最后一行给我下面的编译错误(Visual Studio的2015年):

The last line gives me the following compile error (Visual Studio 2015):

无法隐式转换类型

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

这是显式转换存在(是否缺少强制转换?)

An explicit conversion exists (are you missing a cast?)

但是,如果我写做演员

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



然后我得到在运行时无效转换异常。

then I get an invalid cast exception at runtime.

两个问题:


  • 我如何解决这个问题?是遍历键和滴滴?建立一个新的字典位的唯一方法

  • 为什么我会在第一时间这个问题,即使的HashSet 确实实现了的IEnumerable 接口?

  • How do I solve this? Is the only way to iterate over the keys and build up a new dictionary bit by bit?
  • Why do I get this problem in the first place, even thoughHashSet does implement the IEnumerable interface?

推荐答案

它不工作的原因是,在的IDictionary<的价值; TKEY的,TValue> 不是的共变的(并且也不是关键,出于同样的原因)。如果它被允许存在,那么这段代码编译,但的具有的导致异常:

The reason it doesn't work is that the value in IDictionary<TKey, TValue> is not co-variant (and nor is the key, for the same reasons). If it were allowed to be, then this code would compile, but has to result in an exception:

IDictionary<T, HashSet<T>> foo = new Dictionary<T, HashSet<T>>();
IDictionary<T, IEnumerable<T>> bar = foo;
foo.Add(key, new List<T>());

您会想加入列表< T> 会的工作,因为这将编译给定的值类型是所谓的IEnumerable< T> 。它不能成功,不过,作为的实际的值类型为的HashSet< T>

You'd think adding a List<T> would work, as it would compile given the value type is supposedly IEnumerable<T>. It can't succeed, though, as the actual value type is HashSet<T>.

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

So, yes: the only way is to create a new dictionary.

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

这篇关于C#类型转换:显式类型转换存在,但会抛出一个转换错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆