Unable to enable Enum to Int

I have the following code. I need to create a List from KeyValuePair<string, string> with the name and value of each enum value in the specified enum type.

 public static List<KeyValuePair<string, string>> GetEnumList<TEnum>() where TEnum : struct { if (!typeof(TEnum).IsEnum) throw new ArgumentException("Type must be an enumeration"); List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>(); foreach (TEnum e in Enum.GetValues(typeof(TEnum))) list.Add(new KeyValuePair<string, string>(e.ToString(), ((int)e).ToString())); return list; } 

However, the expression ((int)e).ToString() generates the following error.

Cannot convert type 'TEnum' to 'int'

I'm just trying to use an enum instance for an integer. Can someone tell me why this will not work?

EDIT:

I tried this version:

 enum Fruit : short { Apple, Banana, Orange, Pear, Plum, } void Main() { foreach (var x in EnumHelper.GetEnumList<Fruit>()) Console.WriteLine("{0}={1}", x.Value, x.Key); } public static List<KeyValuePair<string, string>> GetEnumList<TEnum>() where TEnum : struct { if (!typeof(TEnum).IsEnum) throw new ArgumentException("Type must be an enumeration"); List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>(); foreach (TEnum e in Enum.GetValues(typeof(TEnum))) { list.Add(new KeyValuePair<string, string>(e.ToString(), ((int)(dynamic)e).ToString())); } return list; } 

But this gives me an error:

Cannot convert type 'System.Enum' to 'int'

+4
source share
2 answers

TEnum is - for limitation - structure. This will not necessarily be an enumeration.

However, since you apply this restriction at run time, you can use the fact that each enumeration implements IConvertible :

 foreach (IConvertible e in Enum.GetValues(typeof(TEnum))) { list.Add(new KeyValuePair<string, string>( e.ToString(), e.ToType( Enum.GetUnderlyingType(typeof(TEnum)), CultureInfo.CurrentCulture).ToString())); } 

Other approaches, all of which have disadvantages:

You can specify object and then int .

Note that this will happen at runtime if the base type of the enum is something other than int .

This can be overcome by clicking on dynamic before applying to int :

 ((int)(dynamic)e).ToString() 

However, this again has some problems:

If the enumeration is of type long , ulong or uint , it will return incorrect values. You can reduce the problem by choosing ulong instead of int , but will still return invalid values ​​for negative enum values.

+9
source

The only safe way to do this without resorting to the base type is to use the methods created for it! Methods inside the Enum class. Even you can try using the IConvertable interface.

 // Get underlying type, like int, ulong, etc. Type underlyingType = Enum.GetUnderlyingType(typeof(T); // Convert the enum to that type. object underlyingValue = e.ToType(underlyingType, null); // Convert that value to string. string s = underlyingValue.ToString(); 

Or in a word:

 string s = e.ToType(Enum.GetUnderlyingType(typeof(T)), null).ToString(); 

You can implement it in your code as follows:

 public static List<KeyValuePair<string, string>> GetEnumList<TEnum>() where TEnum : struct, IConvertible { if (!typeof(TEnum).IsEnum) throw new ArgumentException("Type must be an enumeration"); List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>(); foreach (TEnum e in Enum.GetValues(typeof(TEnum))) { list.Add(new KeyValuePair<string, string> ( e.ToString(), e.ToType(Enum.GetUnderlyingType(typeof(TEnum)), null).ToString() )); } return list; } 

If you do not want to use IConvertable , try using the Convert class:

 string s = Convert.ChangeType(e, Enum.GetUnderlyingType(typeof(TEnum))).ToString(); 
+3
source

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


All Articles