10

I am trying to make a method to enumerate any enum, by returning a list containing each constant name and value.

Here's my code:

Type enumType = typeof(SomeEnum);
var enumConstants = 
    Enum.GetValues(enumType).
         Cast<enumType>().
         Select(x => new { Value = (int) x, Name = x.ToString() });

(I declare enumType in this snippet but it is in fact declared in my method signature as MyMethod(Type enumType). I am not showing my method signature because that would require to introduce some struct I am using, which is not relevant to the problem here)

The problem is that this code does not compile and I am getting on the Cast<enumType>(). line the following error:

The type or namespace name 'enumType' could not be found (are you missing a using directive or an assembly reference?)

I don't understand how can enumType be unknown, I just declared it on the previous line!

Note that on the Enum.GetValues(enumType). line, there is no error flagged.

Am I missing something here? Is some LINQ playing tricks?

Otiel
  • 18,404
  • 16
  • 78
  • 126

6 Answers6

5

This should be:

Type enumType = typeof(SomeEnum);
var enumConstants = 
    Enum.GetValues(enumType).
     Cast<SomeEnum>().
     Select(x => new { Value = (int) x, Name = x.ToString() });

The problem is the Cast<T>() call. The generic method type needs the actual type specification (SomeEnum), not a System.Type (enumType).

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 2
    I can't do `Cast` by design, because I don't know `SomeEnum` in my method. My method will be called like `GetEnumValues(typeof(SomeEnum))`. – Otiel Dec 15 '11 at 18:54
5

Generics don't allow you to pass an instance of a variable, you'll need to either use Cast<SomeEnum>(), or make the method where this code lies generic, and use something like Cast<T>().

wsanville
  • 37,158
  • 8
  • 76
  • 101
3

Try this:

    Type enumType = typeof(SqlDbType);
    var enumConstants =
        Enum.GetValues(enumType).
             Cast<Enum>().
             Select(x => new { Value = Convert.ToInt32(x), Name = x.ToString() });
iuristona
  • 927
  • 13
  • 30
1

Try changing

Cast<enumType>()

to

Cast<SomeEnum>()
competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • 2
    Can't do that. The purpose of my method is to *not* know what is the *enum*. My method will be called like `GetEnumValues(typeof(SomeEnum))`. Should have made that clearer in the question :) – Otiel Dec 15 '11 at 18:56
1

here is what I have

namespace SO.Enums
{
    public enum SomeEnum
    {
        A,
        B,
        C,
        D
    }

    class Program
    {
        static void Main(string[] args)
        {
            Type enumType = typeof(SomeEnum);
            var enumConstants =
                Enum.GetValues(enumType).
                     Cast<SomeEnum>().
                     Select(x => new { Value = (int)x, Name = x.ToString() });
        }
    }
}
Amir Aliabadi
  • 181
  • 1
  • 4
0
string[] names = Enum.GetNames(typeof(MyEnum));
MyEnum[] values = (MyEnum[])Enum.GetValues(typeof(MyEnum));
int[] intValues = values.Cast<int>().ToArray();
for (int i = 0; i < names.Length; i++) {
    Console.WriteLine("{0} = {1}", names[i], intValues[i]);
}

Or in a more concise way:

var result = Enum.GetValues(typeof(MyEnum))
    .Cast<MyEnum>()
    .Select(x => new { Name = Enum.GetName(typeof(MyEnum), x), Value = (int)x });
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188