?
Solved

How to get variable name as string

Posted on 2007-11-20
3
Medium Priority
?
2,508 Views
Last Modified: 2013-11-07
How do I get variable names in a class as strings through reflection? I know that local variables will be renamed but it should be possible for non-locals.

Thanks
0
Comment
Question by:gromul
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 7

Expert Comment

by:multithreading
ID: 20321929
foreach(FieldInfo field in myType.GetType().GetFields()) Console.WriteLine(field.Name);
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 800 total points
ID: 20321956
Something like this:

For fields:
foreach (System.Reflection.PropertyInfo p in yourClass.GetType().GetFields())
{
      // do something with p.ToString()
}

For properties:
foreach (System.Reflection.PropertyInfo p in yourClass.GetType().GetProperties())
{
      // do something with p.ToString()
}
0
 
LVL 7

Accepted Solution

by:
multithreading earned 1200 total points
ID: 20322019
... and if you don't want to use an instance of your class, preferring to reference the class directly, you could write

foreach(FieldInfo field in typeof(MyClass).GetFields()) Console.WriteLine(field.Name);
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question