Linq ToDictionary不会将类隐式转换为接口

凯扎

我有以下代码

public class TestAdaptor
{
    private interface ITargetClass
    {
        Guid Id { get; }

        string Name { get; }
    }

    private class MyTargetClass : ITargetClass
    {
        public Guid Id { get; private set; }

        public string Name { get; private set; }

        public MyTargetClass(MySourceClass source)
        {
        }
    }

    private class MySourceClass
    {
        public Guid Id { get; set; }

        public string Name { get; set; }
    }

    private Dictionary<Guid, IEnumerable<ITargetClass>> ConvertItems(Dictionary<Guid, IEnumerable<MySourceClass>> source)
    {
        return source.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Select(v => new MyTargetClass(v)));
    }
}

但是,由于ToDictionary行会导致以下错误,因此无法编译

Cannot implicitly convert type
'System.Collections.Generic.Dictionary<System.Guid,System.Collections.Generic.IEnumerable<TestAdaptor.TestAdaptor.MyTargetClass>>'
to
'System.Collections.Generic.Dictionary<System.Guid,System.Collections.Generic.IEnumerable<TestAdaptor.TestAdaptor.ITargetClass>>'   ...\TestAdaptor.cs  38  20

现在,很明显MyTargetClass实现了ITargetClass,但是编译器没有对此进行处理。

现在,我正在显式转换(ITargetClass)new MyTargetClass(v)

但是,为什么会首先发生这种情况,并且有更好的方法来解决此问题?

菲利普·皮特尔

编译器不会自动转换IEnumberable<X>IEnumerable<Y>即使X : Y因为IDictionary不是协变在此讨论其基本原理:IDictionary <,>逆方差?.NET 4中的IDictionary <TKey,TValue>不协变

至于绕过它,就像您提到的那样,您必须强制转换:

使用Cast扩展方法:

 kvp => kvp.Value.Select(v => new MyTargetClass(v)).Cast<ITargetClass>()

显式转换:

 kvp => kvp.Value.Select(v => (ITargetClass) new MyTargetClass(v))

更新

只是因为和之间的混淆IEnumerable对此进行了扩展IDictionaryIEnumerable 协变的。IDictionary 不是

很好:

 IEnumerable<ITargetClass> list = new List<MyTargetClass>();

这不是:

 IDictionary<object, IEnumerable<ITargetClass>> dict = 
     new Dictionary<object, List<MyTargetClass>>();

IDictionary继承自IEnumerable<KeyValuePair<TKey, TValue>>有争议的是KeyValuePair哪个不是协变的,哪个IDictionary不是协变的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用LINQ IEnumerable.ToDictionary()时无法将类隐式转换为基类

Rails:不会将nil隐式转换为String

不会将Symbol隐式转换为整数,嵌套形式

Ruby,不会将Symbol隐式转换为Integer

Linq无法隐式转换类型

无法将类型'System.Linq.IQueryable'隐式转换为'Microsoft.EntityFrameworkCore.Query.IIncludableQueryable'

无法将类型'ulong []'隐式转换为'Newtonsoft.Json.Linq.JToken'

Option Strict导致LINQ语句内的编译错误(将“ Object”隐式转换为“ Control”)

使用 Linq 分组时无法将类型列表隐式转换为 IEnumerable

无法将类型'System.Linq.IQueryable <char []>'隐式转换为'string []'

无法将类型'System.Linq.IQueryable'隐式转换为'System.Collections.Generic.List

无法隐式转换类型列表匿名类型1 linq

Linq.Expressions值类型隐式转换

无法隐式转换类型'System.Linq.IQueryable

SQL转换为LINQ

Lambda转换为LINQ

VS 2017不会将const char *隐式转换为char *

编译sass样式表时,不会将Symbol隐式转换为Integer

不会将toString()方法中的super关键字隐式转换为super.toString()

Rails,mongoid抛出TypeError不会将nil隐式转换为String

将Facter注入文件内容-不会将Hash隐式转换为String

使用Rails 4.0.2和Strong参数不会将Array类型隐式转换为Integer

Rails,在表上使用.find时,不会将Symbol隐式转换为Integer

为什么我会得到“不会将false隐式转换为字符串”

不会将 Symbol 隐式转换为 Integer 只是有时发生

MessagesController#create_message中的TypeError不会将nil隐式转换为String

无法通过linq C#将类型system.collection.generic IEnumerable隐式转换为组中的模型

无法将类型'Newtonsoft.Json.Linq.JObject'隐式转换为'System.Collections.Generic.IEnumerable <Employee>'

无法将类型System.Collections.Generic.Ienumerable <string>隐式转换为system.Linq.IOrderedEnumerable <AnonymousType#1>