32

This question relates to casting of enums within generic methods

Given an enum

public enum Crustaceans
{
    Frog = 1,
    Toad = 4
}

I can create an instance of my enum simply enough

short val = 4;
Crustaceans crusty = (Crustaceans) val;

However, if

short val = 4;
object obj = (object) val;
Crustaceans crusty = (Crustaceans)obj;

a runtime exception is thrown attempting to perform the initialisation of crusty.

Can anyone explain why this is happening, and why it is not legal to do such a thing.

Not that I really wanted to do this, but I cam across an issue when trying to get something similar happening with generics and effectively that is what is happening under the covers. i.e.

public T dosomething<T>(short val) where T : new()
{
    T result = (T)(object) val;
    return result;
}

So what I am attempting to do is have a generic function that works with enums and non-enums (not so critical-but would be nice) that can be set to a short value without throwing an exception and actually initialising the correct enum value.

sweetfa
  • 5,457
  • 2
  • 48
  • 62
  • 37
    Would it be out of line to point out that Frog and Toad are not Crustaceans? :) – JTMon Aug 16 '12 at 08:01
  • 1
    possible duplicate of [Why can't I unbox an int as a decimal?](http://stackoverflow.com/questions/1085097/why-cant-i-unbox-an-int-as-a-decimal) – Fredrik Mörk Aug 16 '12 at 08:03
  • 2
    @sweetfa: while the question I pointed out deals with int vs. decimal and not enums, it does explain the behavior (which is related to boxing and unboxing). In particular, the accepted answer refers to an article by Eric Lippert: "[Representation and Identity](http://blogs.msdn.com/b/ericlippert/archive/2009/03/19/representation-and-identity.aspx)", which explains this in great detail. – Fredrik Mörk Aug 16 '12 at 08:05

6 Answers6

59

Something like this probably will help you:

public T dosomething<T>(object o)
{
   T enumVal= (T)Enum.Parse(typeof(T), o.ToString());
   return enumVal;
}

But this will work only with enums, for clear reason of using Enum.Parse(..)

And use this like, for example:

object o = 4;
dosomething<Crustaceans>(o);

That will return Toad in your case.

Kit
  • 20,354
  • 4
  • 60
  • 103
Tigran
  • 61,654
  • 8
  • 86
  • 123
  • 1
    That can be fixed by surrounding the code with a check for it being an enum or an int like so if(T is enum) – Kjartan Þór Kjartansson Aug 16 '12 at 08:09
  • Improved(?) version: public T dosomething(object o) { var enumType = typeof(T); if (!enumType.IsEnum) throw new ArgumentException("Not an enum"); return (T)Convert.ChangeType(o, enumType); } – osexpert Jul 21 '17 at 00:29
9

In case of integral types boxed as objects the correct way to do the conversion is using Enum.ToObject method:

public T Convert<T>(object o)
{
   T enumVal= (T)Enum.ToObject(typeof(T), o);
   return enumVal;
}
hazzik
  • 13,019
  • 9
  • 47
  • 86
6

There are cases when you can not use Generics (like in a WPF Converter when you get the value as an object). In this case you can not cast to int because the enum type may not be an int. This is a general way to do it without Generics. The Example is given inside a WPF Converter, but the code inside is general:

using System;
using System.Windows;
using System.Windows.Data;

.
.
.

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    var enumType = value.GetType();
    var underlyingType = Enum.GetUnderlyingType(enumType);
    var numericValue = System.Convert.ChangeType(value, underlyingType);
    return numericValue;
}
Tal Segal
  • 2,735
  • 3
  • 21
  • 17
0

By default enum digit values have type = int. In your code you want to cast value that have short type to enum type. For that, you have to set short type to your enum values. This will work:

public enum Crustaceans : short // <--
{
    Frog = 1,
    Toad = 4
}

short  @short = 1;
object @object = @short;

var @enum = (Crustaceans)@object; // @enum = Crustaceans.Frog   

if you don't want to change default enum values type

public enum Crustaceans
{
    Frog = 1,
    Toad = 4
}

object @object = Crustaceans.Frog;

var @enum = (Crustaceans)@object; // @enum = Crustaceans.Frog

or

public enum Crustaceans
{
    Frog = 1,
    Toad = 4
}

int @integer= 1;

var @enum = (Crustaceans)@integer; // @enum = Crustaceans.Frog
Evgeniy Miroshnichenko
  • 1,788
  • 2
  • 19
  • 30
0

Tigrin's answer will convert the string name of the enum to the enum - which may be what is desired, where as Hazzik's will not, which also may be what is desired. But if you want the functionality of Tigrin's answer, with the efficiency of Hazzik's answer (Tigrin's answer takes an enum, converts it to a string and then back again...), this code will do it:

    public static bool TryParseEnum<EnumType>(object obj, ref EnumType enumType) where EnumType : struct, System.IConvertible
    {
        if (obj != null)
        {
            try
            {
                string str = obj as string;
                if (str == null)
                {
                    enumType = (EnumType)Enum.ToObject(typeof(EnumType), obj);
                    return true;
                }
                else
                {
                    str = str.Trim();
                    if (str.Length > 0)//optimization to avoid throwing and catching in frequently occurring case
                    {
                        //if (!int.TryParse(str, out int unused))//ARRRRGGGG!!! Microsoft allows the string "2" to be parsed to the enum, which we never want.  Why would anyone want this???
                        {
                            enumType = (EnumType)Enum.Parse(typeof(EnumType), str, true);
                            return true;
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }
        return false;
    }

Note the code in the commented out code: Tigrin's answer, and the code above, allow "string" representations of integers, e.g. "1", to be converted to an enum. The implementor has to decide if they want to allow that or not.

David I. McIntosh
  • 2,038
  • 4
  • 23
  • 45
-1

You can use this extension:

public static T ToEnum<T>(this object obj)
    {
        var objType = obj.GetType();
        if (typeof(T).IsEnum)
        {
            if (objType == typeof(string))
                return (T)Enum.Parse(typeof(T), obj.ToString());
            return (T)Enum.ToObject(typeof(T), obj);
        }
        if (objType == typeof(string))
            return (T)Enum.Parse(Nullable.GetUnderlyingType(typeof(T)), obj.ToString());
        return (T)Enum.ToObject(Nullable.GetUnderlyingType(typeof(T)), obj);
    }

Enum

public enum Crustaceans { Frog = 1, Toad = 4 }

Usage

Crustaceans x = "Toad".ToEnum<Crustaceans>();
Crustaceans y = 4.ToEnum<Crustaceans>();
if (x == y) 
{
    System.Console.WriteLine("Equale");
}
Yaseen
  • 598
  • 6
  • 7