14

I have a problem working out how exactly to create an instance of an enum when at runtime i have the System.Type of the enum and have checked that the BaseType is System.Enum, my value is an int value matching an item in the mystery Enum.

The code i have so far is just the logic described above as shown below.

        if (Type.GetType(type) != null)
        {
            if (Type.GetType(type).BaseType.ToString() == "System.Enum")
            {
                return ???;
            }
        }

When working with Enums in the past i have always know at code time which enum i am trying to parse but in this scenario im confused and have had little luck articulating my question in a google friendly way... I would usually do something like

(SomeEnumType)int

but since i dont know the EnumType at code time how can i achieve the same thing?

Jarmez De La Rocha
  • 618
  • 2
  • 9
  • 19
  • It is a little confusing what do you want to do after "return ???", and why do you need reflection in this case. You still can do the same (SomeEnumType)type to cast the type to SomeEnumType. – outcoldman Apr 06 '13 at 20:57
  • The problem is i do not know which Enum is could be, at runtime is could be any.. The Return ??? would be something like [code] Enum.Parse(Type.GetType(type), ob);[/code] – Jarmez De La Rocha Apr 06 '13 at 21:01
  • This line `Type.GetType(type).BaseType.ToString() == "System.Enum"` tells me that object `type` already has the type of your `SomeEnumType`, so why do you need to convert it from `SomeEnumType` to `SomeEnumType`? Could you give more background on what do you want to achieve with this? – outcoldman Apr 06 '13 at 21:07
  • yeah actually that just did the trick, answered my own question.. FYI though i am using a row of data containing the system.Type string of the enum and a value in the form of an int to create an instance of whichever enum is required using the value. Thanks for the help though :) – Jarmez De La Rocha Apr 06 '13 at 21:10

2 Answers2

20

Use the ToObject method on the Enum class:

var enumValue = Enum.ToObject(type, value);

Or like the code you provided:

if (Type.GetType(type) != null)
{
    var enumType = Type.GetType(type);
    if (enumType.IsEnum)
    {
        return Enum.ToObject(enumType, value);
    }
}
Dan
  • 9,717
  • 4
  • 47
  • 65
1

Use (ENUMName)Enum.Parse(typeof(ENUMName), integerValue.ToString())

as a generic function (edited to correct syntax errors)...

    public static E GetEnumValue<E>(Type enumType, int value) 
                        where E : struct
    {
        if (!(enumType.IsEnum)) throw new ArgumentException("Not an Enum");
        if (typeof(E) != enumType)
            throw new ArgumentException(
                $"Type {enumType} is not an {typeof(E)}");
        return (E)Enum.Parse(enumType, value.ToString());
    }

old wrong version:

public E GetEnumValue(Type enumType, int value) where E: struct
{
  if(!(enumType.IsEnum)) throw ArgumentException("Not an Enum");
  if (!(typeof(E) is enumType)) 
       throw ArgumentException(string.format(
           "Type {0} is not an {1}", enumType, typeof(E));
  return Enum.Parse(enumType, value.ToString());
}
Charles Bretana
  • 143,358
  • 22
  • 150
  • 216
  • 2
    This seems wrong in a couple of ways. (1) Your method has a `where` constraint, but you forget to "define" the type `E` and make your method generic. (2) How is one supposed to supply `E` when calling the method? It is not going to be inferred by the compiler. (3) Your `is` expression is syntactically wrong. The right-hand side of `is` is an object, not a type. Maybe you meant to use `!=` instead of `is`? What is the purpose? (4) The return type requires you to cast the output from the non-generic `Enum.Parse` method. – Jeppe Stig Nielsen Apr 06 '13 at 22:25
  • Sorry, typing without compile check. Edited to fix syntax errors. – Charles Bretana Jun 22 '16 at 20:48