Sunday 29 October 2017

c# - How can I get an enum name from the name of the enum type and an int

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

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...