我试图在我的应用程序中添加一些类型安全的字典逻辑,并试图寻找实现将给定字典转换为并发字典。在没有运气的情况下搜索了一段时间之后,我最终实现了一个我自己的版本和一些被黑客攻击的代码。下面是我提出的版本:
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>();
任何改进此方法的建议或任何更好的替代方案都可以达到相同的效果?
答案 0 :(得分:1)
查看MSDN,ConcurrentDictionary有一个构造函数,它接受一个IEnumerable的KeyValuePairs。这会实现你想要的吗?
public static ConcurrentDictionary<TKey, TValue> ToConcurrentDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> input)
{
return new ConcurrentDictionary<TKey, TValue>(input);
}
答案 1 :(得分:0)
看起来没有更好的选择。感谢所有分享观点的人。
答案 2 :(得分:0)
//Translate A to B
var a = new Dictionary<Dictionary<string, string>>();
var b = a.SerializeJson()
.DeserializeJson<ConcurrentDictionary<ConcurrentDictionary<string,string>>();