By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,616 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,616 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
maf wrote:
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));
--
mikeb
Nov 15 '05 #2

P: n/a
mikeb wrote:
maf wrote:
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)));
--
mikeb
Nov 15 '05 #3

P: n/a

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[i]);
}

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.

Nov 15 '05 #4

P: n/a
100
Hi maf,
You can get that info using Enum class members.

HTH
B\rgds
100

"maf" <mf*********@proclarity.com> wrote in message
news:9c**************************@posting.google.c om...
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

Nov 15 '05 #5

P: n/a
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
Nov 15 '05 #6

P: n/a
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
<an*******@discussions.microsoft.com> wrote in message
news:14****************************@phx.gbl...
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

Nov 15 '05 #7

P: n/a

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.

Nov 15 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.