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"
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Kaine
  • 1,285
  • 2
  • 16
  • 30
  • 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..? – MethodMan Oct 24 '17 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 – Kaine Oct 24 '17 at 14:59
  • You'd have to use reflection to get the `Cheese` enum and its values, then – Jonesopolis Oct 24 '17 at 15:00
  • string name = ((Cheese)1).ToString(); – jdweng Oct 24 '17 at 15:00
  • The `Enum` class has static helper methods that take a `Type` parameter, ie: https://msdn.microsoft.com/en-us/library/system.enum.getname(v=vs.110).aspx – C.Evenhuis Oct 24 '17 at 15:01
  • MethodMan : Doesn't it take the same amount of time to give the answer then to tell the person to search the web. Your response is useless. It took me 15 seconds to give a good answer. – jdweng Oct 24 '17 at 15:02
  • 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..? ??? – MethodMan Oct 24 '17 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. – jdweng Oct 24 '17 at 15:15
  • @jdweng It took you 15s to give half an answer. You failed to understand the question though. The other half, the part that you did not address, is how to obtain a type from its name. The asker could easily have done a websearch to find out how to do both of these. Of course experts can answer more quickly than a novice can search. So what? This question is trivially solved by websearch. If the asker is incapable of searching for this, then they are not up to being a programmer. It's a dupe many times over. Close it as such. – David Heffernan Oct 25 '17 at 10:26
  • Again David you are wrong. The OPs simple wanted to get the string from the number. – jdweng Oct 25 '17 at 11:01
  • @jdweng Read carefully. Or even read at all. The enum type is not known at compile time. The accepted answer should make it clear to you. I know that you don't like being corrected, but don't let pride get in the way of reason. – David Heffernan Oct 25 '17 at 12:26

3 Answers3

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).

Evk
  • 98,527
  • 8
  • 141
  • 191
4
Type t = Type.GetType(dataType)
string value = Enum.GetName(t, dataValue)

should do the trick (uncompiled, on subway)

Erik A. Brandstadmoen
  • 10,430
  • 2
  • 37
  • 55
  • 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. – Kaine Oct 24 '17 at 15:07
  • 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. – Erik A. Brandstadmoen Oct 24 '17 at 15:09
  • 1
    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. – Kaine Oct 24 '17 at 15:17
0

enum.Parse() would be your friend here.

Like so:

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