I need to convert enum to dictionary and after that format each name (value) of dictionary.
public static Dictionary<int, string> EnumToDictionary<TK>(Func<TK, string > func)
{
if (typeof(TK).BaseType != typeof(Enum))
throw new InvalidCastException();
return Enum.GetValues(
typeof(TK))
.Cast<Int32>()
.ToDictionary(
currentItem => currentItem => Enum.GetName(typeof(TK), currentItem))
/*. func for each name */;
}
public enum Types {
type1 = 0,
type2 = 1,
type3 = 2
}
public string FormatName(Types t) {
switch (t) {
case Types.type1:
return "mytype1";
case Types.type2:
return "mytype2";
case Types.type3:
return "mytype3";
default:
return string.Empty;
}
}
After that I need to do something like this:
var resultedAndFormatedDictionary =
EnumToDictionary<Types>(/*delegate FormatName() for each element of dictionary() */);
How do I define and implement delegate (Func<object, string > func
), which performs some action for each value of dictionary?
UPDATE: Correspoding result is
var a = EnumToDictionary<Types>(FormatName);
//a[0] == "mytype1"
//a[1] == "mytype2"
//a[2] == "mytype3"