How do you get the default value of a Constant using reflection in c#?

M

maf

Using reflection, I'm trying to get the value for a constant in an enum.
Getting the contant name works fine using:

FieldInfo[] fieldInfos = TYPE.GetFields();

foreach(FieldInfo fi in fieldInfos )
{

Console.WriteLine("Name: {0}" fi.Name);
Console.WriteLine("Value: {0} " fi.GetValue(null).ToString());
}

This write:
Name: myFieldName
Value: myFieldName

I want:
Name: myFieldName
Value: 6

Thanks,
mf
 
M

mikeb

maf said:
Using reflection, I'm trying to get the value for a constant in an enum.
Getting the contant name works fine using:

FieldInfo[] fieldInfos = TYPE.GetFields();

foreach(FieldInfo fi in fieldInfos )
{

Console.WriteLine("Name: {0}" fi.Name);
Console.WriteLine("Value: {0} " fi.GetValue(null).ToString());
}

This write:
Name: myFieldName
Value: myFieldName

I want:
Name: myFieldName
Value: 6

Thanks,
mf

Convert the enum value to an int instead of a string:

Console.WriteLine("Value: {0}", (int) fi.GetValue(null));
 
M

mikeb

mikeb said:
maf said:
Using reflection, I'm trying to get the value for a constant in an enum.
Getting the contant name works fine using:

FieldInfo[] fieldInfos = TYPE.GetFields();

foreach(FieldInfo fi in fieldInfos )
{

Console.WriteLine("Name: {0}" fi.Name);
Console.WriteLine("Value: {0} " fi.GetValue(null).ToString());
}

This write:
Name: myFieldName
Value: myFieldName

I want:
Name: myFieldName
Value: 6

Thanks,
mf


Convert the enum value to an int instead of a string:

Console.WriteLine("Value: {0}", (int) fi.GetValue(null));

A slightly more correct but complex form (which won't break if the
underlying enum type can't be cast into an int):

Console.WriteLine("real value: {0}",
Convert.ChangeType( fi.GetValue(null),
Enum.GetUnderlyingType( fi.FieldType)));
 
J

Jeffrey Tan[MSFT]

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(Such as string representaion, value, type,
etc..)

================================================
Actually, FieldInfo.GetValue will return the an object which is a type of
your enumeration. And FieldInfo.GetValue().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.GetNames(typeof(test));
for(int i=0;i<names.Length;i++)
{
Console.WriteLine(names);
}

Array arr=Enum.GetValues(typeof(test));
for(int i=0;i<arr.Length;i++)
{
Console.WriteLine(((int)arr.GetValue(i)).ToString());
}

Second Solution(Use Reflection):
Type t=typeof(test);
FieldInfo [] fis=t.GetFields();
foreach(FieldInfo fi in fis)
{
if(fi.Name!="value__")
{
object obj=fi.GetValue(null);
Console.WriteLine(((int)obj).ToString());
}
}
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.
 
G

Guest

Jeff,
Thanks for the reply. Before I got this(which worked as
well), I had found that
fi.GetValue(null).GetHashCode());
worked.
But don't you think that GetValue() should implicitely
retun a value, not an object?
Thanks for your time.
-mf
 
1

100

Hi,
The most convenient way to work with enumerators , I believe, is to use Enum
class members as I suggested.
However you can get the value using reflection as well.
fi.GetValue(null) returns object of eunumerations type.
For example
enum MyEnum {First = 100, Sec}
Type t = typeof(MyEnum);
FieldInfo fi = t.GetField("First");
object val = fi.GetValue(null);

at this point we have val set to an object, which actual type is MyEnum. If
I try to do val.ToString() I'll get the string "First" because that's how
enum's ToString method works.
However enums have underlaying type and enum objects can be cast to them. In
my case underlaying type is int
so I can do
((int)val).ToString(); and this will give me the string "100".
If it has to work in all casses underalying type doesn't have to be
hardcoded. Some of the method of more safe type casting should be used.

HTH
B\rgds
100
 
J

Jeffrey Tan[MSFT]

Hi MF,

Thanks very much for your feedback.
I am glad my reply makes sense to you. I think your further is that: Why
FieldInfo.GetValue() not return a "value" but an object type.

=============================================
Actually, FieldInfo class discovers the attributes of a field and provides
access to field metadata. It is a common usage class for class fields, not
special for Enum.
For other class, its fields may be of any other types, so the only suitable
type to refer the return value of FieldInfo.GetValue is System.Object.(If
the field is primitive type, it will get boxed to reference type)

So I recommand you to use Enum.GetValues() method to do this, because it is
specially for Enumeration. The Enum.GetValues() will return an array
objects with the type of your enumeration. System.Enum inherited from
System.ValueType which is all the value type's base class.

=============================================

If you still have anything unclear, please feel free to tell me, I will
work with you.
Have a nice day!

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top