Accessing the name of a variable via reflection

K

Ken Durden

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
 
E

Eric Newton

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]
 
K

Ken Durden

Eric Newton said:
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
 
M

Mattias Sjögren

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
 

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

Top