473,394 Members | 1,701 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,394 software developers and data experts.

Accessing the name of a variable via reflection

Hey,

I posted this a few months ago and got completely off-the-chart
responses. At the time I wasn't using reflection very much, but more
and more I find myself wanting to use it to write flexible
implementations of IComparable or Min/Max based only on FieldInfos or
PropertyInfos, but I still find the fact that I have to hardcore a
string with the field / property name unbearable.

I re-read my post and it still seems intelligeble to me, so I thought
I'd re-post and see if I got any different responses.

I'm all but completely sure that this feature does _not_ exist in C#,
but I think its important enough to ensuring compile-time checking of
names that its worth getting this idea out there again and maybe MS
could even pick up on it.

Just to re-iterate, the goal of this is avoiding hardcoded strings
like "FunctionName", because someone could later change that to
"MethodName", and hit Build, fix all the compiler errors and end up
not changing the place where "FunctionName" has been hardcoded as a
string literal until the code explodes at a customer site.

Thanks,
-ken

-- Repost --

Hey,

I want to access the name of a member variable at runtime. My impetus
for
doing this is to avoid type-o issues which could result in exceptions
at runtime.

class A
{
public long _aaa;
public long _abc;
public long _xyz;
}

void f()
{
A a = new A();

string s = magicFunction( a._abc );
Debug.Assertion( s == "_abc" );

string s = magicFunction( a._aaa );
Debug.Assertion( s == "_aaa" );

string s = magicFunction( a._xyz );
Debug.Assertion( s == "_xyz" );

The actual goal for doing this is to get a FieldInfo to that object.
Currently, I can do the following:

System.Reflection.FieldInfo = typeof( A ).FieldInfo( "_abc" );

But if this is used in too many places, I'm worried about typeos and
variable name changes not being detected at compile time. The final
form of what I want would look this:

System.Reflection.FieldInfo xyzField
= typeof( A ).FieldInfo( magicFunction( a._xyz ) );

Please implement magicFunction for me, or tell me why it can't be
done. Something fairly close could be done in C++ using macros, but I
would get "a._xyz" as a string, which I would then have to parse out
the "_xyz" part by knowing that "." is a delimiting token.

Thanks,
-ken
Nov 15 '05 #1
3 10892
There's no way to guarantee that the string literal "FunctionName" through
reflection will be binarily compatible or even give the correct operation as
"MethodName" later...

Its just not possible to make that kind of guarantee... if you figure out a
way, you'll be richer then Bill Gates due to the fact that you would have
achieved a nirvana of sorts in the computer industry... a "god program" that
can determine before you even write the program if it both works and gives
the right outcome... [this is not a sarcastic remark... just illustrating a
point]
--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
er**@cc.ensoft-software.com [remove the first "CC."]

"Ken Durden" <cr*************@hotmail.com> wrote in message
news:18**************************@posting.google.c om...
Hey,

I posted this a few months ago and got completely off-the-chart
responses. At the time I wasn't using reflection very much, but more
and more I find myself wanting to use it to write flexible
implementations of IComparable or Min/Max based only on FieldInfos or
PropertyInfos, but I still find the fact that I have to hardcore a
string with the field / property name unbearable.

I re-read my post and it still seems intelligeble to me, so I thought
I'd re-post and see if I got any different responses.

I'm all but completely sure that this feature does _not_ exist in C#,
but I think its important enough to ensuring compile-time checking of
names that its worth getting this idea out there again and maybe MS
could even pick up on it.

Just to re-iterate, the goal of this is avoiding hardcoded strings
like "FunctionName", because someone could later change that to
"MethodName", and hit Build, fix all the compiler errors and end up
not changing the place where "FunctionName" has been hardcoded as a
string literal until the code explodes at a customer site.

Thanks,
-ken

-- Repost --

Hey,

I want to access the name of a member variable at runtime. My impetus
for
doing this is to avoid type-o issues which could result in exceptions
at runtime.

class A
{
public long _aaa;
public long _abc;
public long _xyz;
}

void f()
{
A a = new A();

string s = magicFunction( a._abc );
Debug.Assertion( s == "_abc" );

string s = magicFunction( a._aaa );
Debug.Assertion( s == "_aaa" );

string s = magicFunction( a._xyz );
Debug.Assertion( s == "_xyz" );

The actual goal for doing this is to get a FieldInfo to that object.
Currently, I can do the following:

System.Reflection.FieldInfo = typeof( A ).FieldInfo( "_abc" );

But if this is used in too many places, I'm worried about typeos and
variable name changes not being detected at compile time. The final
form of what I want would look this:

System.Reflection.FieldInfo xyzField
= typeof( A ).FieldInfo( magicFunction( a._xyz ) );

Please implement magicFunction for me, or tell me why it can't be
done. Something fairly close could be done in C++ using macros, but I
would get "a._xyz" as a string, which I would then have to parse out
the "_xyz" part by knowing that "." is a delimiting token.

Thanks,
-ken

Nov 15 '05 #2
"Eric Newton" <er**@cc.ensoft-software.com> wrote in message news:<ez**************@TK2MSFTNGP12.phx.gbl>...
There's no way to guarantee that the string literal "FunctionName" through
reflection will be binarily compatible or even give the correct operation as
"MethodName" later...

Its just not possible to make that kind of guarantee... if you figure out a
way, you'll be richer then Bill Gates due to the fact that you would have
achieved a nirvana of sorts in the computer industry... a "god program" that
can determine before you even write the program if it both works and gives
the right outcome... [this is not a sarcastic remark... just illustrating a
point]


Sigh,

Although what you are saying is technically correct, I think you're
missing the point entirely. This is exactly the reason I do _not_ want
to use textual literals for my function names.

An answer which seems to me could easily be built into the .NET
Reflection API is this:

string strFunctionName = A.FunctionName.Name;

That way, if someone changes FunctionName to MethodName, it is
detected by the god program known as a compiler.

-ken
Nov 15 '05 #3
Ken,
Just to re-iterate, the goal of this is avoiding hardcoded strings
like "FunctionName", because someone could later change that to
"MethodName", and hit Build, fix all the compiler errors and end up
not changing the place where "FunctionName" has been hardcoded as a
string literal until the code explodes at a customer site.


Ensuring that the name exists is only part of the problem. For this to
be really useful, you would also need to verify that the signature is
correct. Becuase if you expect a Foo(int) and there exists a
Foo(string), the compile time name check will do you no good.

You also have to handle different kinds of identifiers. If you want to
verify that a field called _abc exists, you would probably have to
specify that only fields should be considered, and not a property
called _abc.

What you're suggesting is probably doable, but I doubt that it's worth
implementing, considering the complexity of getting it right and how
rarely it's needed.

Perhaps an add-in would be a better solution?

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: mcdonamw | last post by:
This may sound like a stupid stupid question and I figure it would b more "general" than pertaining to a specific Language. I'm using vb.net and I have a bunch of Const values in my program. can...
4
by: Lucas Sain | last post by:
Hi, I think thta for this I have to use reflection... but I'm not shure. How can I find/get an object at runtime by looking for its name that is stored in a variable. For example: I have a...
9
by: Rakesh | last post by:
Hi, I am able to obtain a MenuItem object's Name property @ design-time, but am not able to get the same @ run- time...why? And since MenuItem doesn't inherit from Control class, it's not...
2
by: S Shulman | last post by:
Hi Is there any way of finding out the name of a variable at Runtime? I am working on solution to add all member variables of a class to a collection and write them to file in pairs of...
4
by: Hugh O'Donnell | last post by:
I am trying to access the values of child properties by a string value. For instance, have a user type "Form1.txtFirstName.Text" in a text box, click on a button, and return the value of that...
3
by: Olivier BESSON | last post by:
Hello, I have a web service of my own on a server (vb.net). I must declare it with SoapRpcMethod to be used with JAVA. This is a simple exemple method of my vb source : ...
2
by: Rafe Culpin | last post by:
Does anyone please know of a way to access static methods of a class, when the name of that class is held in a variable? I have several classes (PHP5) which all have identically named methods and...
4
by: raj_genius | last post by:
I hav two queries, whc are as follows: FIRSTLY: is it possible to access the controls(by name) of a parent form(MDI) from its child forms??if yes then how??plzz provide a coded example in VB if...
45
by: Zytan | last post by:
Shot in the dark, since I know C# doesn't have macros, and thus can't have a stringizer operator, but I know that you can get the name of enums as strings, so maybe you can do the same with an...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.