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

Reading the name of a variable

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


P: n/a
> 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/de...ieldstopic.asp

http://msdn.microsoft.com/library/de...classtopic.asp
Nov 17 '05 #2

P: n/a
> 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/de...ieldstopic.asp

http://msdn.microsoft.com/library/de...classtopic.asp
Nov 17 '05 #3

P: n/a
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" <no****@spam.jp> wrote in message
news:OZ**************@TK2MSFTNGP15.phx.gbl...
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/de...ieldstopic.asp

http://msdn.microsoft.com/library/de...classtopic.asp

Nov 17 '05 #4

P: n/a
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" <no****@spam.jp> wrote in message
news:OZ**************@TK2MSFTNGP15.phx.gbl...
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/de...ieldstopic.asp

http://msdn.microsoft.com/library/de...classtopic.asp

Nov 17 '05 #5

P: n/a


Ignacio Machin ( .NET/ C# MVP ) wrote:
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" <no****@spam.jp> wrote in message
news:OZ**************@TK2MSFTNGP15.phx.gbl...
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/de...ieldstopic.asp

http://msdn.microsoft.com/library/de...classtopic.asp


Nov 17 '05 #6

P: n/a


Ignacio Machin ( .NET/ C# MVP ) wrote:
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" <no****@spam.jp> wrote in message
news:OZ**************@TK2MSFTNGP15.phx.gbl...
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/de...ieldstopic.asp

http://msdn.microsoft.com/library/de...classtopic.asp


Nov 17 '05 #7

P: n/a
Larry Lard <la*******@hotmail.com> wrote:
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...


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

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

P: n/a
Larry Lard <la*******@hotmail.com> wrote:
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...


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

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

This discussion thread is closed

Replies have been disabled for this discussion.