Home > Net >  How to implement similar nameof method
How to implement similar nameof method

Time:01-10

Most of the time, we need to get the name of the variable itself, can use the nameof access, for example, we have a class:
Public class MyClass
{
Public string Name {get; set; }
}
At the same time, we write a method, business process, need to use the name of the variable itself as a parameter (just sample, don't tangle method itself have any meaning) :
Private string Test (string text)
{
//test, direct return
return text;
}
When used with the following method invocation:
Var STR=Test ( nameof (MyClass. Name));//STR="Name";
So in the nameof way, seem a bit repetitive, especially frequent use is not very convenient (actual is often and string combination, pieced together after not intuitive), I hope that a similar method call:
Var STR=Test ( MyClass. Name );
Ignore the nameof method, direct incoming MyClass. Name, excuse me, how to write the Test method?

CodePudding user response:

Very simple, you write a c # compiler, let Microsoft to buy your compiler instead..net compiler,

CodePudding user response:

A lot of similar problems are such as if 2002 with 2020 c # 9.0 version of the grammar, time travel to "the future", you can!
  •  Tags:  
  • C#
  • Related