473,382 Members | 1,258 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

reflection question

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
2 2425
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: A. Wiebenga | last post by:
Hi all! I am a student at the Hogeschool van Arnhem en Nijmegen in Holland. I am currently involved in a research project regarding Reflection. Purpose of the research project is to document...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
1
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am...
7
by: John | last post by:
I have a class the reads in a file and sets the values of the file into its properties. This class is used to populate the data onto a form. This form has controls created at runtime based on...
1
by: Pieter Breed | last post by:
Hi All, I am trying to use the attribute/reflection system to as much potential as I can think of, but I've run into a snag. I would appreciate it if someone would point me in the right...
5
by: heddy | last post by:
I understand that reflection allows me to discover the metadata of a class at runtime (properties, methods etc). What I don't understand is where this is useful. For example: If I am the sole...
5
by: =?Utf-8?B?Q2hyaXN0aWFuIEhhdmVs?= | last post by:
Hi, is it faster to access the fields from an object directly by using reflection than accessing them by their properties using reflection? Thanks Christian
17
by: raylopez99 | last post by:
What good is C# Reflection, other than to find out what types are in an assembly? And to dynamically invoke methods in an assembly (.dll or .exe)? Also, bonus question, can you use Reflection...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.