Reading the name of a variable

  • Thread starter Thread starter Azat
  • Start date Start date
A

Azat

Hi,
I am wondering if there is a way in C# to get the name of a variable. Let's
say:

int MyName=3;

How can I get a string with the Value "MyName"? Any ideas on that?

Thanks

Azat
 
Hi,
I am wondering if there is a way in C# to get the name of a variable. Let's
say:

int MyName=3;

How can I get a string with the Value "MyName"? Any ideas on that?

You can get it by using reflection.

Type aType = Type.GetType("YourClassName");
FieldInfo[] fields = aType.GetFields();

FieldInfo has property Name.

http://msdn.microsoft.com/library/d...f/html/frlrfsystemtypeclassgetfieldstopic.asp

http://msdn.microsoft.com/library/d.../frlrfsystemreflectionfieldinfoclasstopic.asp
 
Hi,

This should only work with the variable is a member of a type, it will not
work if the variable is declared inside a method for example.


Also this do not solve the OP's problem he want to know the name of a
particular variable, not to get a list of the fields of a type.


AFAIK, there is no way to know the name of a variable.


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Lebesgue said:
Hi,
I am wondering if there is a way in C# to get the name of a variable. Let's
say:

int MyName=3;

How can I get a string with the Value "MyName"? Any ideas on that?

You can get it by using reflection.

Type aType = Type.GetType("YourClassName");
FieldInfo[] fields = aType.GetFields();

FieldInfo has property Name.

http://msdn.microsoft.com/library/d...f/html/frlrfsystemtypeclassgetfieldstopic.asp

http://msdn.microsoft.com/library/d.../frlrfsystemreflectionfieldinfoclasstopic.asp
 
Ignacio said:
Hi,

This should only work with the variable is a member of a type, it will not
work if the variable is declared inside a method for example.


Also this do not solve the OP's problem he want to know the name of a
particular variable, not to get a list of the fields of a type.


AFAIK, there is no way to know the name of a variable.

This would make sense; after all the semantics at runtime shouldn't
depend on irrelevant details of the source, right? I mean, presumably
if you take a program and just change all the (private) variable names,
you will compile to identical IL...


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Lebesgue said:
Hi,
I am wondering if there is a way in C# to get the name of a variable. Let's
say:

int MyName=3;

How can I get a string with the Value "MyName"? Any ideas on that?

You can get it by using reflection.

Type aType = Type.GetType("YourClassName");
FieldInfo[] fields = aType.GetFields();

FieldInfo has property Name.

http://msdn.microsoft.com/library/d...f/html/frlrfsystemtypeclassgetfieldstopic.asp

http://msdn.microsoft.com/library/d.../frlrfsystemreflectionfieldinfoclasstopic.asp
 
Larry Lard said:
This would make sense; after all the semantics at runtime shouldn't
depend on irrelevant details of the source, right? I mean, presumably
if you take a program and just change all the (private) variable names,
you will compile to identical IL...

No - the private type/member variable names are still present in the
IL, but not *local* variable names.
 

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

Back
Top