views:

67

answers:

4

I have a simple Enum

 public enum TestEnum
 {
     TestOne = 3,
     TestTwo = 4
 }

var testing = TestEnum.TestOne;

And I want to retrieve its value (3) via reflection. Any ideas on how to do this?

Thanks Mat

+2  A: 

No need for reflection:

int value = (int)TestEnum.TestOne;
Fredrik Mörk
yet again I know how to do it this way. but I need to use reflection
mjmcloug
@mjmcloug: if you extend your question with brief information on *why* you need to use reflection and your use case, you will get better answers :)
Fredrik Mörk
+2  A: 

Why do you need reflection?

int value = (int)TestEnum.TestOne;
Daniel A. White
Great minds think alike. And at the same second, it seems :)
Fredrik Mörk
;) yeah I know how to do it this way. But I need to use reflection
mjmcloug
A: 

Full code : How to Get Enum Values with Reflection in C#

MemberInfo[] memberInfos = typeof(MyEnum).GetMembers(BindingFlags.Public | BindingFlags.Static);
string alerta = "";
for (int i = 0; i < memberInfos.Length; i++) {
alerta += memberInfos[i].Name + " - ";
alerta += memberInfos[i].GetType().Name + "\n";
}
Pranay Rana
A: 

this article is best for you using reflection with Enums: Enum Reflection

Akyegane