Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create list of enums and pass it to a method

Tags:

c#

enums

generics

I created a method which takes an enum and transforms it in a Dictionary where each int is associated with the name (as string) of the enum

// Define like this
public static Dictionary<int, string> getDictionaryFromEnum<T>()
{
   List<T> commandList = Enum.GetValues(typeof(T)).Cast<T>().ToList();
   Dictionary<int, string> finalList = new Dictionary<int, string>();
   foreach (T command in commandList)
   {
    finalList.Add((int)(object)command, command.ToString());
   }
 return finalList;
 }

(ps. yes, I have a double cast but the application is meant to be a very cheap-and-dirty C#-enum to Javascript-enum converter).

This can be easily used like this

private enum _myEnum1 { one = 1001, two = 1002 };
private enum _myEnum2 { one = 2001, two = 2002 };
// ... 
var a = getDictionaryFromEnum<_myEnum1>();
var b = getDictionaryFromEnum<_myEnum2>();

Now, I was wondering whether I could create a list of enums to use for a series of calls to iterate my calls.

This was in the original question: [Why can't I call this?]

What should I do to be able to create a call like this one?

List<Type> enumsToConvertList = new List<Type>();
enumsToConvertList.Add(typeof(_myEnum1));
enumsToConvertList.Add(typeof(_myEnum2));
// this'll be a loop
var a = getDictionaryFromEnum<enumsToConvertList.ElementAt(0)>();
like image 370
malber Avatar asked Feb 04 '13 16:02

malber


People also ask

How do I create an enum list?

The idea is to use the Enum. GetValues() method to get an array of the enum constants' values. To get an IEnumerable<T> of all the values in the enum, call Cast<T>() on the array. To get a list, call ToList() after casting.

Can we create list of enum in Java?

In Java 10 and later, you can conveniently create a non-modifiable List by passing the EnumSet . The order of the new list will be in the iterator order of the EnumSet . The iterator order of an EnumSet is the order in which the element objects of the enum were defined on that enum.

How do I get all the values in an enum list?

Use a list comprehension to get a list of all enum values, e.g. values = [member. value for member in Sizes] . On each iteration, access the value attribute on the enum member to get a list of all of the enum's values.


1 Answers

You can't specify generic argument type at runtime (well, without reflection). So, simply create non-generic method, which accepts argument of Type type:

public static Dictionary<int, string> getDictionaryFromEnum(Type enumType)
{
    return Enum.GetValues(enumType).Cast<object>()
               .ToDictionary(x => (int)x, x => x.ToString());
}

Usage:

List<Type> enumsToConvertList = new List<Type>();
enumsToConvertList.Add(typeof(_myEnum1));
enumsToConvertList.Add(typeof(_myEnum2));

var a = getDictionaryFromEnum(enumsToConvertList[0]);
like image 70
Sergey Berezovskiy Avatar answered Sep 27 '22 21:09

Sergey Berezovskiy