找到传递给函数在C#中的变量名 [英] Finding the Variable Name passed to a Function in C#

查看:106
本文介绍了找到传递给函数在C#中的变量名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我用下面的例子来解释我的问题:

 公共字符串ExampleFunction(字符串变量){
    返回的东西;
}字符串WhatIsMyName =Hello World的;
字符串Hello = ExampleFunction(WhatIsMyName);

当我通过变量WhatIsMyName的例子功能,我希望能够拿到原始变量名的字符串。也许是这样的:

  Variable.OriginalName.ToString()

有没有办法做到这一点?


解决方案

我不这么认为。

您使用的变量名是为了您的方便和可读性。编译器并不需要它和放大器;只是夹头出来,如果​​我没有记错。

如果有帮助,你可以定义一个名为NamedParameter带属性的名称和参数新类。然后,您身边传递这个对象作为参数。<​​/ P>

Let me use the following example to explain my question:

public string ExampleFunction(string Variable) {
    return something;
}

string WhatIsMyName = "Hello World"';
string Hello = ExampleFunction(WhatIsMyName);

When I pass the variable "WhatIsMyName" to the example function, I want to be able to get a string of the original variables name. Perhaps something like:

Variable.OriginalName.ToString()

Is there any way to do this?

解决方案

No.I don't think so.

The variable name that you use is for your convenience and readability. The compiler doesn't need it & just chucks it out if I'm not mistaken.

If it helps, you could define a new class called NamedParameter with attributes Name and Param. You then pass this object around as parameters.

这篇关于找到传递给函数在C#中的变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆