Home >Backend Development >C++ >How Can I Get the Name of a C# Variable or Parameter at Runtime?

How Can I Get the Name of a C# Variable or Parameter at Runtime?

Linda Hamilton
Linda HamiltonOriginal
2025-01-27 03:51:12395browse

How Can I Get the Name of a C# Variable or Parameter at Runtime?

Get variables and parameters in C#

Question statement:

Determine the name of the variable or parameter passed to the function. For example, given the following code:

How do we get the name "MyINPUT" in two cases?

<code class="language-csharp">var myInput = "input";
var nameOfVar = GETNAME(myInput); // ==> nameOfVar 应该等于 "myInput"

void testName([Type?] myInput)
{
   var nameOfParam = GETNAME(myInput); // ==> nameOfParam 应该等于 "myInput"
}</code>
Solution:

C# 6.0 Pre -plan:

Using the MEMBERINFOGetting class, you can retrieve the name of any member provided. The following is the implementation method:

C# 6.0 and higher version scheme:

C# 6.0 The Nameof operational symbol introduced in the search parameters, variables, and attribute names. The following is the implementation method:

The above is the detailed content of How Can I Get the Name of a C# Variable or Parameter at Runtime?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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