开发者

C# Get var name to string

开发者 https://www.devze.com 2022-12-21 01:40 出处:网络
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.

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?

0

精彩评论

暂无评论...
验证码 换一张
取 消