简体   繁体   English

实现通用的 ToConcurrentDictionary 扩展方法

[英]Implementing a generic ToConcurrentDictionary extension method

I Was trying to add some type safe dictionary logic to my app and tried to look around for an implementation to convert a given dictionary to a concurrent one.我试图向我的应用程序添加一些类型安全的字典逻辑,并试图寻找一种将给定字典转换为并发字典的实现。 After searching for a while with no luck, I ended up implementing a version of my own with some hacked up code.在没有运气的情况下搜索了一段时间后,我最终用一些修改过的代码实现了我自己的一个版本。 below is the version I have come up with:下面是我想出的版本:

    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`"));
        }
}

Using the method is like below:使用方法如下:

  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>();

Any suggestions to improve this method or any better alternatives to achieve the same results?有什么建议可以改进这种方法或有更好的替代方法来达到相同的结果吗?

Looking at MSDN there is a constructor for ConcurrentDictionary that accepts an IEnumerable of KeyValuePairs.查看 MSDN,有一个 ConcurrentDictionary 的构造函数,它接受 KeyValuePairs 的 IEnumerable。 Would this achieve what you're looking for?这会实现你想要的吗?

public static ConcurrentDictionary<TKey, TValue> ToConcurrentDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> input)
{
        return new ConcurrentDictionary<TKey, TValue>(input);
}
//Translate A to B
var a = new Dictionary<Dictionary<string, string>>();
var b = a.SerializeJson()
         .DeserializeJson<ConcurrentDictionary<ConcurrentDictionary<string,string>>();

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我知道这是一个旧线程,但是查看 ToDictionary 的 Microsoft 源代码,我能够为ToConcurrentDictionaryToConcurrentBag制作扩展器

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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM