tval
tval

Reputation: 592

Get list of enums from enum without type

Using reflection, I have an enum value. How can I get the list of all enums from that value without knowing the type of the Enum?

Here is some pseudo code:

List<string> enumList = Enum
    .GetValues(enumVal.GetType())
    .Cast<MyEnum>()
    .Select(v => v.ToString())
    .ToList();

In the code example, this would work if I knew that enumVal was MyEnum, but I don't: it could be any Enum (MyEnum, YourEnum, etc)

Upvotes: 0

Views: 982

Answers (1)

Holger
Holger

Reputation: 2662

You can take

Enum.GetNames(enumVal.GetType())

to get an array of string values, add .ToList(); if you want a list.

Upvotes: 1

Related Questions