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

reflection question

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


P: n/a
Dan <fu***@iol.it> wrote:
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.

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

P: n/a

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" <fu***@iol.it>
| 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: <#M**************@TK2MSFTNGP12.phx.gbl>
| 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!TK2MSFTN GP12.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!
|
|
|

Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.