greetings, could you please help? i need to get a variable (name) in string format. for example below i need to display "test" as string.
int test = 69;
//below does not work
Messag开发者_运维知识库eBox.Show((string)test);
// below works but displays the int value
MessageBox.Show(test.ToString());
thank you for your time.
well i have an enum:
public enum ShipOrientation
{
North,
East,
South,
West
}
and i do some processing based on direction and if int North then apply enum North direction.
not for variables, but if you promote your variable to a property on the class, you can use Expressions to achieve this.
good sample here
This cannot be done normally - variable names are compiled out in C#.
On the other hand, if you can write the variable's name, you can just place quotation marks around it and treat it as a string.
Assuming that's not good enough for your scenario, there are a few hacks that can let you do something almost like this; but their usefulness kind of depends on what you're trying to do - why do you need to variable's name?
精彩评论