Hi mf,
Thank you for using MSDN Newsgroup! My name is Jeffrey, and I will be
assisting you on this issue.
Based on my understanding, you want to use .Net Reflection to get some
information of your enumeration(Suc h as string representaion, value, type,
etc..)
=============== =============== =============== ===
Actually, FieldInfo.GetVa lue will return the an object which is a type of
your enumeration. And FieldInfo.GetVa lue().ToString( ) will default return
the string representation of your enumeration value(That is the field
name). To get the actual int value of your enumeration, you can just
explicit convert your enumeration to int type.
You can try the following 2 Solutions to see if it helps resolve your issue:
enum test
{
test1=1,
test2=55,
test3=67,
test4=89
};
First Solution(Use Enumeration class methods):
string [] names=Enum.GetN ames(typeof(tes t));
for(int i=0;i<names.Len gth;i++)
{
Console.WriteLi ne(names[i]);
}
Array arr=Enum.GetVal ues(typeof(test ));
for(int i=0;i<arr.Lengt h;i++)
{
Console.WriteLi ne(((int)arr.Ge tValue(i)).ToSt ring());
}
Second Solution(Use Reflection):
Type t=typeof(test);
FieldInfo [] fis=t.GetFields ();
foreach(FieldIn fo fi in fis)
{
if(fi.Name!="va lue__")
{
object obj=fi.GetValue (null);
Console.WriteLi ne(((int)obj).T oString());
}
}
Note: Use reflection, you will get the 5 fieldinfo objects instead of 4.
Because it will get an internal object in fis[0](Its name is value__), we
just jump over this object.
=============== =============== ===========
Please apply my suggestion above and let me know if it helps resolve your
problem.
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Hope you have a nice experience in Microsoft Newsgroup!
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.