Reputation: 1149
Example:
class foo
{
byte val = 3;
string EnumName = "boo";
Enum boo : byte
{
coo = 1,
doo = 2,
hoo = 3
}
Enum boo2 : byte
{
eoo = 3,
goo = 8
}
}
How can I do something like this:
Console.WriteLine((GetEnum(EnumName)value).ToString()); // GetEnum is not real
The EnumName will change everytime.
Expected Output:
When Enum Name is boo == "hoo"
When Enum Name is boo2 == "eoo"
Edit: I am going to use this for logging:
Message to boo2.goo
Message to boo.coo
Message to boo.doo
Upvotes: 0
Views: 162
Reputation: 360
If you want to get a type based on the string you could search your whole application domain for the available types and you could check if the type is an enumeration as well as if the name suites. After you have the right type you should be able to get the values / names, whatever you want. Maybe this solution is a little bit like a hammer, but I think it works for your case. (I dont know how your application is structured).
var domain = AppDomain.CurrentDomain;
var assemblies = domain.GetAssemblies();
foreach(var assembly in assemblies)
{
foreach(Type t in assembly.GetTypes())
{
string name = t.Name; // or t. Fullname if you know it
// you can also check if the type is an Enum, etc...
}
}
Upvotes: 0