0

I have a generic method which will return a select list populated from an enum.

public static IEnumerable<SelectListItem> GetGenericEnumSelectList<T>()
{
     return (Enum.GetValues(typeof(T)).Cast<int>().Select(e => new SelectListItem() { Text = EnumExtensions.GetEnumDescription((ProductsEnum)e) , Value = e.ToString() })).ToList();
}

I have put a call to a static method I also have called GetEnumDescription, this expects an Enum. How can I pass in the current type rather than hard coding the type as I have done. So that if T is a different type of enum, it will be generic.

Something like:

public static IEnumerable<SelectListItem> GetGenericEnumSelectList<T>()
{
    return (Enum.GetValues(typeof(T)).Cast<int>().Select(e => new SelectListItem() { Text = EnumExtensions.GetEnumDescription((typeof(T))e) , Value = e.ToString() })).ToList();
}

EDIT

Signature of GetEnumDescription is

public static string GetEnumDescription(Enum value)
{

///

}
JsonStatham
  • 9,770
  • 27
  • 100
  • 181
  • What is the signature of `GetEnumDescription`? I bet it´ll contain `Enum` as first parameter, not the *actual* type of enum. So there´s no need to cast to the specific type at all. – MakePeaceGreatAgain Jun 14 '18 at 13:00
  • 1
    you can cast using generic type as any other type `(T)e` – Rafal Jun 14 '18 at 13:00
  • @Rafal That assumes there´s a generic constraint for enum, [which it is not](https://stackoverflow.com/questions/79126/create-generic-method-constraining-t-to-an-enum). – MakePeaceGreatAgain Jun 14 '18 at 13:02
  • e is an int at that point, I get "Argument type 'T' is not assignable to parameter type 'System.Enum' – JsonStatham Jun 14 '18 at 13:03
  • Useful in C#7.3: [Enum constraint](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/constraints-on-type-parameters#enum-constraints) – Hans Kesting Jun 14 '18 at 13:03
  • 2
    Why not simply cast to `Enum` if your signature needs `Enum` anyway? – MakePeaceGreatAgain Jun 14 '18 at 13:03
  • "Argument type `T` is not assignable to parameter type `System.Enum`" Because `T` can be *everything*, not just an enum. Unfortunately there´s no generic constraint for enum. – MakePeaceGreatAgain Jun 14 '18 at 13:05

1 Answers1

3

Unfortunately there´s no generic constraint for enums - at least not before C#7.3. So the following isn´t possible:

void SoSomething<T>(T myEnum) where T: Enum { }

You could add another constraint as mentioned here:

void SoSomething<T>(T myEnum) where T: struct, IConvertible{ }

However that won´t help you much.

However in your case there´s no need to cast at all, as your methods parameter is Enum. So you can simply cast e to Enum:

return (Enum.GetValues(typeof(T)).Cast<Enum>().Select(e => new SelectListItem { 
    Text = EnumExtensions.GetEnumDescription(e), 
    Value = Convert.ToInt32(e).ToString() 
})).ToList();

Bw aware on the cast to Enum. Otherwise you get the following error:

Cannot convert type int to System.Enum

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111