Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Question on Reflection..

69 views
Skip to first unread message

farseer

unread,
Sep 11, 2005, 8:25:40 PM9/11/05
to
Hi,
I have a class which defines a few integer Constants:
public class AppConst
{
public const int XVALUE = 100;
public const int YVALUE = 200;
public const int YVALUE = 300;
}

I'd like to write a procedure in which i can pass the constant name as
a string and be able to retrieve the constant value. for instance
//method definition
public int getConst( string constName ){...}
//usage
int x = getConst( "XVALUE" );

I assume i can use reflection to do this. Can someone tell me how
please?
(The reason i want to do this is that i want to use the constant name
in a properties file rather than the value)

thank you

Jon Skeet [C# MVP]

unread,
Sep 11, 2005, 8:58:12 PM9/11/05
to

using System;
using System.Reflection;

public class Constants
{
public const int XValue = 10;
public const int YValue = 20;
public const int ZValue = 30;
}

class Test
{
static void Main()
{
Console.WriteLine (GetConst("XValue"));
Console.WriteLine (GetConst("YValue"));
}

static object GetConst (string name)
{
FieldInfo field = typeof (Constants).GetField
(name,
BindingFlags.Static |
BindingFlags.Public);
return field.GetValue(null);
}
}

--
Jon Skeet - <sk...@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

farseer

unread,
Sep 11, 2005, 9:11:57 PM9/11/05
to
thanks! i'm going to try this. Question, when you say
"typeof(Constants)", in my case, that would be "typeof(AppConst)",
correct?

Jon Skeet [C# MVP]

unread,
Sep 11, 2005, 9:43:15 PM9/11/05
to

Yes. Note that I renamed your constants to fit in with the .NET naming
convention. You don't have to do that, but I'd recommend it :)

farseer

unread,
Sep 11, 2005, 9:49:48 PM9/11/05
to
thank you...standards are good and so i appreciate that note. i will
modify my names accordingly..thank you!

farseer

unread,
Sep 14, 2005, 11:46:55 PM9/14/05
to
hi, this works great. what if i now wanted the string representation
of a constant, given it's numeric value. this is so i can now save the
constant name back to a file, rather than the integer value...

farseer

unread,
Sep 14, 2005, 11:58:22 PM9/14/05
to
Hi Jon,
this worked perfectly. I have to additional question:

1. Is it possible to use this mechanism to retrieve Enum constants?
2. Is it possible to do the reverse, that is given an int value,
retrieve the string representation of the constant that represents that
int value?

thanks much for your help!

farseer

unread,
Sep 15, 2005, 12:05:18 AM9/15/05
to
I figured out how to do #1, i simply need to qualify with CLASNAME.ENUM
name:

static object GetConst (string name)
{

FieldInfo field = typeof (Constants.MyEnumName).GetField


(name,
BindingFlags.Static |
BindingFlags.Public);
return field.GetValue(null);
}

Still not sure how to do #2 however.

Jon Skeet [C# MVP]

unread,
Sep 15, 2005, 1:48:26 AM9/15/05
to
farseer <far...@optonline.net> wrote:

<snip>

> 2. Is it possible to do the reverse, that is given an int value,
> retrieve the string representation of the constant that represents that
> int value?

Well, if you know all the places that constant could live, you can find
all the fields (using GetFields), fetch all the values and find out
which is equal to the value you've got. Just be aware that you could
easily have multiple fields with the specified value.

0 new messages