Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert.ChangeType and converting to enums?

I got an Int16 value, from the database, and need to convert this to an enum type. This is unfortunately done in a layer of the code that knows very little about the objects except for what it can gather through reflection.

As such, it ends up calling Convert.ChangeType which fails with an invalid cast exception.

I found what I consider a smelly workaround, like this:

String name = Enum.GetName(destinationType, value); Object enumValue = Enum.Parse(destinationType, name, false); 

Is there a better way, so that I don't have to move through this String operation?

Here's a short, but complete, program that can be used if anyone need to experiment:

using System;  public class MyClass {     public enum DummyEnum     {         Value0,         Value1     }      public static void Main()     {         Int16 value = 1;         Type destinationType = typeof(DummyEnum);          String name = Enum.GetName(destinationType, value);         Object enumValue = Enum.Parse(destinationType, name, false);          Console.WriteLine("" + value + " = " + enumValue);     } } 
like image 220
Lasse V. Karlsen Avatar asked Feb 03 '09 13:02

Lasse V. Karlsen


People also ask

How do I convert string to enum?

Use the Enum. IsDefined() method to check if a given string name or integer value is defined in a specified enumeration. Thus, the conversion of String to Enum can be implemented using the Enum. Parse ( ) and Enum.

What is the method to convert an enum type to a string type?

There are two ways to convert an Enum to String in Java, first by using the name() method of Enum which is an implicit method and available to all Enum, and second by using toString() method.

Can we convert enum to string?

We can convert an enum to string by calling the ToString() method of an Enum.

Are enums thread safe C#?

Well by default, enum is of type int, 32bit, unless explicitly specified otherwise. That means, that reading/writing is atomic => thread-safe.


1 Answers

Enum.ToObject(.... is what you're looking for!

C#

StringComparison enumValue = (StringComparison)Enum.ToObject(typeof(StringComparison), 5); 

VB.NET

Dim enumValue As StringComparison = CType([Enum].ToObject(GetType(StringComparison), 5), StringComparison) 

If you do a lot of Enum converting try using the following class it will save you alot of code.

public class Enum<EnumType> where EnumType : struct, IConvertible {      /// <summary>     /// Retrieves an array of the values of the constants in a specified enumeration.     /// </summary>     /// <returns></returns>     /// <remarks></remarks>     public static EnumType[] GetValues()     {         return (EnumType[])Enum.GetValues(typeof(EnumType));     }      /// <summary>     /// Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.     /// </summary>     /// <param name="name"></param>     /// <returns></returns>     /// <remarks></remarks>     public static EnumType Parse(string name)     {         return (EnumType)Enum.Parse(typeof(EnumType), name);     }      /// <summary>     /// Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.     /// </summary>     /// <param name="name"></param>     /// <param name="ignoreCase"></param>     /// <returns></returns>     /// <remarks></remarks>     public static EnumType Parse(string name, bool ignoreCase)     {         return (EnumType)Enum.Parse(typeof(EnumType), name, ignoreCase);     }      /// <summary>     /// Converts the specified object with an integer value to an enumeration member.     /// </summary>     /// <param name="value"></param>     /// <returns></returns>     /// <remarks></remarks>     public static EnumType ToObject(object value)     {         return (EnumType)Enum.ToObject(typeof(EnumType), value);     } } 

Now instead of writing (StringComparison)Enum.ToObject(typeof(StringComparison), 5); you can simply write Enum<StringComparison>.ToObject(5);.

like image 101
Peter Avatar answered Sep 30 '22 17:09

Peter