Get the original variable name passed to the function
In C#, it is very common to pass variables as parameters as parameters. However, obtaining the original name of the transmission variable may be tricky. Consider the following example:
When using variable calls
1 2 3 4 5 6 7 | public string ExampleFunction(string Variable)
{
return "something" ;
}
string WhatIsMyName = "Hello World" ;
string Hello = ExampleFunction(WhatIsMyName);
|
Copy after login
, we may want to get the name of the original variable, for example:
WhatIsMyName
ExampleFunction
Can this be implemented in C#?
<<>
1 | Variable.OriginalName.ToString()
|
Copy after login
Although the original variable name cannot be obtained directly, the C# 3.0 and higher versions can use LAMBDA expressions to provide a solution:
Note:
This method depends on unspecified behavior and may not be compatible with the future version or other compilers of C#. It is only suitable for direct access variables. If the variables pass or conversion through any calculation or conversion, this method will fail. In addition, this technology is mainly used for reflection and code analysis, and it is not recommended to use in conventional code.
The above is the detailed content of How Can I Retrieve the Original Variable Name Passed to a C# Function?. For more information, please follow other related articles on the PHP Chinese website!