Question on Reflection..

F

farseer

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
 
J

Jon Skeet [C# MVP]

farseer said:
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)

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);
}
}
 
F

farseer

thanks! i'm going to try this. Question, when you say
"typeof(Constants)", in my case, that would be "typeof(AppConst)",
correct?
 
J

Jon Skeet [C# MVP]

farseer said:
thanks! i'm going to try this. Question, when you say
"typeof(Constants)", in my case, that would be "typeof(AppConst)",
correct?

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 :)
 
F

farseer

thank you...standards are good and so i appreciate that note. i will
modify my names accordingly..thank you!
 
F

farseer

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...
 
F

farseer

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!
 
F

farseer

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.
 
J

Jon Skeet [C# MVP]

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.
 

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