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