reflection question

D

Dan

Let's say I have a class like:

class Dummy
{
public const string CONE = "one";
public const string CTWO = "two";

... other stuff ....
}

How can I use reflection to get the value of the class constants given a
string corresponding to the constant name? E.g. starting from the string:

string s = "CONE";

I'd like to retrieve the value of Dummy's constant named CONE (the value of
s), i.e. "one". Could anyone give a sample or point me to some useful
tutorial about reflection?

Thanx!
 
J

Jon Skeet [C# MVP]

Dan said:
Let's say I have a class like:

class Dummy
{
public const string CONE = "one";
public const string CTWO = "two";

... other stuff ....
}

How can I use reflection to get the value of the class constants given a
string corresponding to the constant name? E.g. starting from the string:

string s = "CONE";

I'd like to retrieve the value of Dummy's constant named CONE (the value of
s), i.e. "one".

Do you know in advance (at compile time) that the type is dummy? If so,
just use:

string value = (String) typeof(Dummy).GetField(s).GetValue(null);

You probably want to do some error checking in that though (e.g. check
that GetField doesn't return null, and that the returned value isn't
null) if you're getting the name from the user.
Could anyone give a sample or point me to some useful
tutorial about reflection?

If you look at the reflection overview in MSDN, then follow the links,
most classes and methods have samples with them.
 
J

Jeffrey Tan[MSFT]

Hi Dan,

Thanks for posting in this group.
Jon has provided you a plain sample to you through reflection.
If your assembly has not been loaded, you should use Assembly.Load static
to load this assembly, then you can use Assembly.GetType("Dummy") to get
"Dummy" type. Then you can follow Jon's code to get the const value.
In the GetValue method, its parameter is the instance object of "Dummy".
Because you want to get the const value(static field) of the class, you
should pass it "null".

Actually, the reflection use the manifest and the metadata to get the
information of the assembly, you can use the ildasm.exe tool followed by
.Net to get the manifest and metadata of an assembly.
I think once you have a clear view of assembly structure, you just need to
look for related classes and methods in System.Reflection namespace in MSDN.
You also can find quick start samples in www.gotdotnet.com at the link
below:
http://samples.gotdotnet.com/quickstart/howto/

If you still have any question, please feel free to let me know.

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.

--------------------
| From: "Dan" <[email protected]>
| Subject: reflection question
| Date: Mon, 3 Nov 2003 11:16:30 +0100
| Lines: 22
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: ppp-217-133-158-110.cust-adsl.tiscali.it
217.133.158.110
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:196207
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Let's say I have a class like:
|
| class Dummy
| {
| public const string CONE = "one";
| public const string CTWO = "two";
|
| ... other stuff ....
| }
|
| How can I use reflection to get the value of the class constants given a
| string corresponding to the constant name? E.g. starting from the string:
|
| string s = "CONE";
|
| I'd like to retrieve the value of Dummy's constant named CONE (the value
of
| s), i.e. "one". Could anyone give a sample or point me to some useful
| tutorial about reflection?
|
| Thanx!
|
|
|
 

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