Convert Generic T to System.Enum

I have an Enum list as an IEnumerable<T> , and I need to loop on each element and get its description as follows:

 IEnumerable<T> values = (T[])Enum.GetValues(typeof(T)); foreach (Enum value in values) { String mylist = Common.MyExtensions.getEnumDescription(value); } ... public static string getEnumDescription(Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes != null && attributes.Length > 0) { return attributes[0].Description; } else return value.ToString(); } 

This results in an error in the foreach section

cannot convert "T" to System.Enum.

Isn't an IEnumerable System.Enum List First? What kind of actors can do the trick?

+6
source share
5 answers

Isn't the IEnumerable System.Enum List First? What kind of cast can do the trick?

Yes, but the compiler cannot verify this. T can be any at runtime. In such situations, common type restrictions are usually used, but where T : Enum not valid, so you can do the following:

  • Do not share your method; change the parameter type to Enum if you want to work only with Enums
  • Use the where T : struct to at least make sure that T is a value type and check if the Enum type is inside your method if it does not throw an exception, etc. (this is not recommended)
+4
source

One way to do the cast:

 Enum enumValueError = (Enum)value; //compiler error: Cannot convert type 'xxx' to 'System.Enum' Enum enumValueNoError = value as Enum; //no error, but enumValueNoError will be null if value is not an Enum 
+2
source

Consider using the type T you are already working with ...

 IEnumerable<T> values = (T[])Enum.GetValues(typeof(T)); foreach (T value in values) { String mylist = Common.MyExtensions.getEnumDescription(value); } 

You will also need to generate getEnumDesciption.

0
source

I think you are looking for something like this (slightly modified implementation):

 public enum Test { [Description("This")] A, B, C, D } private IEnumerable<string> GetEnumDescription<T>() { var type = typeof(T); if (!type.IsEnum) throw new ArgumentException("Only Enum types allowed"); foreach (var value in Enum.GetValues(type).Cast<Enum>()) { yield return getEnumDescription(value); } } public string getEnumDescription(Enum value) { FieldInfo fi = value.GetType().GetField(value.ToString()); DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attributes != null && attributes.Length > 0) { return attributes[0].Description; } else { return value.ToString(); } } 

And the call will look like this:

 var desc = GetEnumDescription<Test>(); // "This", "B", "C", "D" 
0
source
 foreach(var e in values) { if(e is Enum) { Enum eAsEnum = (Enum)e; String mylist = Common.MyExtensions.getEnumDescription(eAsEnum); } } 
-1
source

Source: https://habr.com/ru/post/978605/


All Articles