2

我试图向我的应用程序添加一些类型安全的字典逻辑,并试图四处寻找将给定字典转换为并发字典的实现。在没有运气的情况下搜索了一段时间后,我最终用一些破解的代码实现了我自己的版本。以下是我想出的版本:

    public static class Extensions
      {
        public static ConcurrentDictionary<TKey, TValueResult> ToConcurrentDictionary<TKey, TValueInput, TValueResult>(this IEnumerable<KeyValuePair<TKey, TValueInput>> input)
        {
          var result = new ConcurrentDictionary<TKey, TValueResult>();
          foreach (var kv in input)
          {
            if (typeof(TValueInput).IsDictionaryType())
            {
              var mi = MethodInfo.GetCurrentMethod() as MethodInfo;
              var valGenericArgs = typeof(TValueInput).GetGenericArguments();
              if (valGenericArgs.Length > 0 && valGenericArgs.Last().IsDictionaryType())
              {
                Type generic = typeof(ConcurrentDictionary<,>);
                var conDicType = generic.MakeGenericType(typeof(TValueResult).GetGenericArguments().Last().GetGenericArguments());
                valGenericArgs = valGenericArgs.Concat(new Type[] { conDicType }).ToArray();
              }
              else
              {
                valGenericArgs = valGenericArgs.Concat(new Type[] { valGenericArgs.Last() }).ToArray();
              }
              var genMi = mi.MakeGenericMethod(valGenericArgs);
              var newDic = genMi.Invoke(null, new object[] { kv.Value });
              result.TryAdd(kv.Key, (TValueResult)newDic);
            }
            else
            {
              result.TryAdd(kv.Key, (TValueResult)Convert.ChangeType(kv.Value, typeof(TValueResult)));
            }
          }
          return result;
        }

        public static bool IsDictionaryType(this Type type)
        {
          return type.FullName.StartsWith("System.Collections.Generic.IDictionary`")
            || type.GetInterfaces().Any(t => t.FullName.StartsWith("System.Collections.Generic.IDictionary`"));
        }
}

使用方法如下:

  var dic1 = new Dictionary<string, Dictionary<int, IDictionary<string, int>>> 
    { 
        {"one", new Dictionary<int, IDictionary<string, int>>
                {
                    {11, new Dictionary<string,int>
                                {
                                    {"one-one-one", 10000}
                                }
                    }
                }
        }
    };
  var dic2 = new Dictionary<int, IDictionary<int, IDictionary<int, string>>> 
    { 
        {1, new Dictionary<int, IDictionary<int, string>>
                {
                    {11, new Dictionary<int,string>
                                {
                                    {111, "one-one-one"}
                                }
                    }
                }
        }
    };

  var dic3 = new Dictionary<int, string> 
                                {
                                    {111, "one-one-one"}
                                };

  var cd1 = dic1.ToConcurrentDictionary<string, Dictionary<int, IDictionary<string, int>>, ConcurrentDictionary<int, ConcurrentDictionary<string, int>>>();
  var cd2 = dic2.ToConcurrentDictionary<int, IDictionary<int, IDictionary<int, string>>, ConcurrentDictionary<int, ConcurrentDictionary<int, string>>>();
  var cd3 = dic3.ToConcurrentDictionary<int, string, string>();

有什么建议可以改进这种方法或任何更好的替代方法来达到相同的结果?

4

3 回答 3

2

查看 MSDN 有一个 ConcurrentDictionary 的构造函数,它接受 KeyValuePairs 的 IEnumerable。这会达到你想要的吗?

public static ConcurrentDictionary<TKey, TValue> ToConcurrentDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> input)
{
        return new ConcurrentDictionary<TKey, TValue>(input);
}
于 2014-02-19T16:01:10.013 回答
0
//Translate A to B
var a = new Dictionary<Dictionary<string, string>>();
var b = a.SerializeJson()
         .DeserializeJson<ConcurrentDictionary<ConcurrentDictionary<string,string>>();
于 2014-04-05T03:31:49.797 回答
0

I know this is an old thread, but looking at the Microsoft source code for ToDictionary, I was able to make an extender for ToConcurrentDictionary and ToConcurrentBag

public static ConcurrentDictionary<TKey, TSource> ToConcurrentDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
    {
        return ToConcurrentDictionary<TSource, TKey, TSource>(source, keySelector, IdentityFunction<TSource>.Instance, null);
    }

    public static ConcurrentDictionary<TKey, TSource> ToConcurrentDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
    {
        return ToConcurrentDictionary<TSource, TKey, TSource>(source, keySelector, IdentityFunction<TSource>.Instance, comparer);
    }

    public static ConcurrentDictionary<TKey, TElement> ToConcurrentDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector)
    {
        //return ToConcurrentDictionary<TSource, TKey, TElement>(source, keySelector, elementSelector, null);
        if (source == null) throw new Exception("Source is null");
        if (keySelector == null) throw new Exception("Key is null");
        if (elementSelector == null) throw new Exception("Selector is null");

        ConcurrentDictionary<TKey, TElement> d = new ConcurrentDictionary<TKey, TElement>();
        foreach (TSource element in source) d.TryAdd(keySelector(element), elementSelector(element));
        return d;
    }

    public static ConcurrentDictionary<TKey, TElement> ToConcurrentDictionary<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
    {
        if (source == null) throw new Exception("Source is null");
        if (keySelector == null) throw new Exception("Key is null");
        if (elementSelector == null) throw new Exception("Selector is null");

        ConcurrentDictionary<TKey, TElement> d = new ConcurrentDictionary<TKey, TElement>(comparer);
        foreach (TSource element in source) d.TryAdd(keySelector(element), elementSelector(element));
        return d;
    }
    public static ConcurrentBag<TSource> ToConcurrentBag<TSource>(this IEnumerable<TSource> source)
    {
        if (source == null) throw new Exception("Source is null");
        return new ConcurrentBag<TSource>(source);
    }
于 2020-01-20T14:38:06.373 回答