public static Dictionary<TKey, TVal> ConvertDictionaryKeys<TKey, TVal>(Dictionary<string, TVal> obj) where TKey : struct
{
Dictionary<TKey, TVal> result = new Dictionary<TKey, TVal>();
TKey[] collection = (TKey[])Enum.GetValues(typeof(TKey));
HashSet<TKey> values = new HashSet<TKey>(collection);
foreach (var entry in obj)
{
TKey? success = null;
foreach (TKey val in values)
{
string name = val.GetAttribute<EnumMemberAttribute>().Value;
if (entry.Key.Equals(name, StringComparison.OrdinalIgnoreCase))
{
result.Add(val, entry.Value);
success = val;
break;
}
}
if (success != null) values.Remove(success.Value);
}
return result;
}