4

From another layer in an application I can get back the name of the enum type and an integer value. How can I use this data to get the string representation of that value for that enum.

For example, if I have an enum:

public enum Cheese
{
    Edam = 1,
    Cheddar = 3,
    Brie = 201,
    RedLeicester = 1001
}

and the data I have available is

string dataType = "Cheese";
int dataValue = 3;

How can I get the result:

"Cheddar"
11
  • 2
    there are plenty of examples on how to return / get the name of an enum on the internet.. have you executed a fruitful search..? Commented Oct 24, 2017 at 14:58
  • Most of the results I get assume you have the type of the enum at compile time - which in this case, I don't Commented Oct 24, 2017 at 14:59
  • You'd have to use reflection to get the Cheese enum and its values, then Commented Oct 24, 2017 at 15:00
  • 1
    @jdweng, so I guess that we should just give everyone the answer to any and all questions that are posted here..if that's the case then why should any of us be programmers / developers ... when we can just rely on others to do the work for us..? ??? Commented Oct 24, 2017 at 15:04
  • 1
    The problem is beginners don't know enough to even figure out what to use to search the web. Even experience people don't always know the key words to find the info on the web. Giving a stupid answer like post code where beginners don't have have code and then when they post stupid code giving them negative points does nothing fruitful. Commented Oct 24, 2017 at 15:15

3 Answers 3

7

In addition to the name of enum itself you should know namespace and assembly that enum is declared at. Assuming you run code below in assembly where enum is defined and all your enums are defined in the same namespace you know at compile time, you can do it like that:

string dataType = "Cheese";
int dataValue = 3;
var enumType = Type.GetType("Namespaces.Of.Your.Enums." + dataType);
var name = Enum.GetName(enumType, dataValue);

If your enum is in another assembly than this code is running at - you will need to provide assembly qualified name of your enum type (again with namespace).

Sign up to request clarification or add additional context in comments.

Comments

4
Type t = Type.GetType(dataType)
string value = Enum.GetName(t, dataValue)

should do the trick (uncompiled, on subway)

3 Comments

This assumes you have an instance of the type in a variable dataType. In my situation I only have the string "Cheese" so Type would be string. Getting the type of the actual enum is the part of the process I'm missing.
No, Type.GetType gets a type from a type name. See @Evk answer for more elaborate explanation. You need to add namespace to the type name, if any.
Ah yes - qualifying the type name is what's required for this - I think I misread that as using dataType.GetType() for some reason! My bad.
0

enum.Parse() would be your friend here.

Like so:

int cheeseType = 1;
(Cheese)Enum.Parse(typeof(Cheese), cheeseType.ToString());

3 Comments

This assumes you have Cheese as a type at compile time. in my situation "Cheese" is a string. It's the reflection to get the type that I'm missing
This won't work. You only have the name of the Cheese type, not the type itself.
Gah you're right I missed that point

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.