Property name

G

Guest

Hi,

class Resource // This class is autogenerated by Visual Studio.
{
internal static string M001
{
get{ return ResourceManager.GetString("M001"); }
}
}


public Main()
{
/*
Logging a message from resources.
If I hover the mouse over M001 in Visual Studio, the tooltip says
"Looks up a localized message similar to MESSAGE TEXT COMES HERE".
*/
LogMessage( Resource.M001 );
}

public LogMessage( ?SomeType? param )
{
/******
I don't want to get "param" as a string here. Instead, I want to
get the name of the property passed as a parameter to this method. Something
like:
******/

Console.Write( param.(MethodInfo or whatever).Name ); // Prints
"M001".
}


In other words, I want to:

- Pass a property (Resource.M001) to a method directly, as a C# identifier,
as this provides compiler type checking and context help.
- In the called method, I want to get the passed property's name ("M001"),
as this name contains encoded additional information about the string the
property retrieves from resources.

I wonder if the above is possible in C# 2.0.

Thanks
 
B

Bruce Wood

Hi,

class Resource // This class is autogenerated by Visual Studio.
{
internal static string M001
{
get{ return ResourceManager.GetString("M001"); }
}

}

public Main()
{
/*
Logging a message from resources.
If I hover the mouse over M001 in Visual Studio, the tooltip says
"Looks up a localized message similar to MESSAGE TEXT COMES HERE".
*/
LogMessage( Resource.M001 );

}

public LogMessage( ?SomeType? param )
{
/******
I don't want to get "param" as a string here. Instead, I want to
get the name of the property passed as a parameter to this method. Something
like:
******/

Console.Write( param.(MethodInfo or whatever).Name ); // Prints
"M001".

}

In other words, I want to:

- Pass a property (Resource.M001) to a method directly, as a C# identifier,
as this provides compiler type checking and context help.
- In the called method, I want to get the passed property's name ("M001"),
as this name contains encoded additional information about the string the
property retrieves from resources.

I wonder if the above is possible in C# 2.0.

Nope. Sorry.

Passing Resource.M001 to a method results in a pass-by-value,
something being pushed on the stack. Once your method starts running,
there is no longer any record of where that value came from. (Apart
from doing some horrible, painful, stack walking and decompiling
stuff.)

Even if you were to pass it as "ref" (which you can't do with
properties anyway), you still couldn't figure out (easily) from which
type the value (to which you now have a reference) came, and which
property within that type was used to give you the value.

So... sorry. No dice.
 
W

Walter Wang [MSFT]

Hi,

I'm with Mark here if our understanding of your question is right. Please
feel free to let us know if you think we may have misunderstood your
question.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Thanks Bruce and Walter. Bruce's answer ("not possible") is what I expected
to get anyway. I wanted to avoid hard-coding M001 etc. prefixes into
resource text, but that's what I ended up with.
 

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