4

Is there a way to create a method that gets an enum type as a parameter, and returns a generic list of the enum underlying type from it's values, no matter if the underlying type is int\short byte etc'...
I saw this answer of Jon Skeet, but it looks way too complicated.

asked Oct 31, 2011 at 9:03
2
  • Example input and output? i.e. do you want the list typed as SomeEnum, or as byte etc? Commented Oct 31, 2011 at 9:06
  • @MarcGravell as I wrote, I want the generic type to be of the underlying type - byte. Commented Oct 31, 2011 at 9:08

2 Answers 2

5

If you want to pass in a Type, it can't really be usefully generic - you'd have to return a single type that isn't directly related to the input, hence something like:

 public static Array GetUnderlyingEnumValues(Type type)
 {
 Array values = Enum.GetValues(type);
 Type underlyingType = Enum.GetUnderlyingType(type);
 Array arr = Array.CreateInstance(underlyingType, values.Length);
 for (int i = 0; i < values.Length; i++)
 {
 arr.SetValue(values.GetValue(i), i);
 }
 return arr;
 }

This is a strongly-typed vector underneath, so you could cast that to int[] etc.

answered Oct 31, 2011 at 9:11
Sign up to request clarification or add additional context in comments.

2 Comments

You can set the method to get the Enum type as a generic param, but it won't change a thing, right?
@gdoron indeed, have it <TEnumType> wouldn't help us, because we aren't returning TEnumType[], but rather an int[], byte[], etc - which we can't tell via the generics.
2

While Marc's answer isn't wrong its somewhat unnecessary. Enum.GetValues(type) returns a TEnum[] so this method is kind of unnecessary as if you know the underlying type you can just cast TEnum[] to its underlying type array.

var underlyingArray = (int[])Enum.GetValues(typeof(StringComparison));

is valid C# that will compile and won't throw an exception at runtime. Since you wanted a list once you have the array you can pass it to the List<Tunderlying> constructor or you can just call the ToArray() extension method.

Edit: you could write the function as such::

public static TUnderlying[] GetValuesAs<TUnderlying>(type enumType)
{
 return Enum.GetValues(enumType) as TUnderlying[];
}

But then you would have to know the underlying type first.

answered Nov 10, 2011 at 15:55

2 Comments

Wow thanks! But still you can't return the strongly type list a function, right?
That's correct Enums are not generic types in the traditional way, if enums were generic like say MyEnum: Enum<int> then this would be possible.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.