Home > Backend Development > C++ > How Can I Retrieve the Original Variable Name Passed to a C# Function?

How Can I Retrieve the Original Variable Name Passed to a C# Function?

Patricia Arquette
Release: 2025-01-29 17:46:10
Original
488 people have browsed it

How Can I Retrieve the Original Variable Name Passed to a C# Function?

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() // == "WhatIsMyName"

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template