将嵌套字典转换为IReadOnlyDictionary

将嵌套字典转换为IReadOnlyDictionary

本文介绍了将嵌套字典转换为IReadOnlyDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试给出一个IReadOnly引用内部Collection对象。
这在大多数情况下运行良好,但如果我想将包含集合的字典转换为包含IReadOnlyCollection的IReadOnlyDictionary,则不行。

I am trying to give out a IReadOnly-references to internal Collection objects.This works well in most cases, but does not if i want to convert a dictionary containing a collection into an IReadOnlyDictionary containing a IReadOnlyCollection.

这里有代码示例:

    var list = new List<int>();
    IReadOnlyList<int> listReference = list; //works;

    var dictionary = new Dictionary<int, int>();
    IReadOnlyDictionary<int, int> dictionaryReference = dictionary; //works

    var nestedList = new List<List<int>>();
    IReadOnlyList<IReadOnlyList<int>> nestedReadOnlyListReference = nestedList; //works

    var nestedDictionary = new Dictionary<int, List<int>>();
    //IReadOnlyDictionary<int, IReadOnlyList<int>> nestedReadOnlyDictionaryReference = nestedDictionary; //does not work, can not implicitly convert

    //current workaround
    var nestedDictionaryReferenceHelper = new Dictionary<int, IReadOnlyList<int>>();
    foreach (var kvpNestedDictionary in nestedDictionary)
    {
        nestedDictionaryReferenceHelper.Add(kvpNestedDictionary.Key, (IReadOnlyList<int>)kvpNestedDictionary.Value);
    }
    IReadOnlyDictionary<int, IReadOnlyList<int>> nestedReadOnlyDictionaryReference = nestedDictionaryReferenceHelper; //works, but is only a reference to the internal List, not to the dictionary itself

解决方法是非常丑陋的,因为它需要额外的内存,需要手动更新每次 nestedDictionary 的值更改。

The Workaround is pretty ugly as it needs additional memory and needs manual updating every time the values of nestedDictionary change.

是否任何简单的转换这种嵌套字典的方法?

Is there any simple way to convert such nested dictionaries?

推荐答案

在这个SO问题中,你可以找到一个很好的解释为什么不支持铸造字典值。请参阅Eric Lippert接受的答案。

In this SO question you can find a very good explanation why casting dictionary values is not supported. Please see the accepted answer of Eric Lippert.

尽管我不会推荐这个,您可以使用以下LINQ表达式将字典的值转换为只读列表:

Although i would not recommend this, you could use the following LINQ expression to cast the values of the dictionary to a read only list:

IReadOnlyDictionary<int, IReadOnlyList<int>> nestedReadOnlyDictionaryReference = nestedDictionary.ToDictionary(kv => kv.Key, kv => kv.Value as IReadOnlyList<int>);

这是一个较短的版本的解决方法,它是懒惰的评估,但我不会推荐这个原因如下:

It is a shorter version of your workaround and it is lazy evaluated, but i would not recommend this due to the following reasons:


  1. 此解决方案仍然会创建字典的副本,并且不会从原始字典更新任何新的/已删除的条目。

  2. 字典的值,即只读列表,是指字典中只读版本中更新的原始列表和更改。

这是不一致的行为,因此是一个不好的做法!

除非是不可能投下字典的值,我不会建议这样做。您应该深入复制整个字典,包括嵌套列表,或使用支持转换的其他容器。

Unless it is not possible to cast the values of a dictionary, i would not recommend doing this. You should either deep copy the entire dictionary including the nested lists, or use an other container that supports casting.

这篇关于将嵌套字典转换为IReadOnlyDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!