0

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"
Alexandre
  • 13,030
  • 35
  • 114
  • 173

1 Answers1

1

Guessing from your question you want to achieve to create a dictionary from an enum where the enum value as int is the key and some formatted name for it is the value, right?

Well, first the function you pass in should take TK as argument, shouldn't it?
Second, throwing a InvalidCastException seems a bit strange. An InvalidOperationException might be more appropriate (see also this question)
Third ToDictionary is already quite close:

public static Dictionary<int, string> EnumToDictionary<TK>(Func<TK, string> func)
{
     if (typeof(TK).BaseType != typeof(Enum))
        throw new InvalidOperationException("Type must be enum");

     return Enum.GetValues(typeof(TK)).Cast<TK>().ToDictionary(x => Convert.ToInt32(x), x => func(x));
}

Now you can call it like this:

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

var resultedAndFormatedDictionary = EnumToDictionary<Types>(FormatName);
Community
  • 1
  • 1
ChrisWue
  • 18,612
  • 4
  • 58
  • 83
  • It doesn't compile - 'System.Linq.ParallelEnumerable.Cast(System.Linq.ParallelQuery)' is a 'method', which is not valid in the given context' – Alexandre Apr 12 '11 at 09:31