你如何得到一个变量的名字,因为它是在它的声明中物理输入的? [英] How do you get a variable's name as it was physically typed in its declaration?

查看:26
本文介绍了你如何得到一个变量的名字,因为它是在它的声明中物理输入的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

可能的重复:
在C#中查找传递给函数的变量名

下面的类包含字段城市.

我需要动态确定字段的名称,因为它是在类声明中键入的即我需要从对象 city 的实例中获取字符串city".

我尝试通过在 DoSomething() 中检查其类型来实现此目的,但在调试器中检查类型的内容时找不到它.

有可能吗?

公共类人{公共字符串城市=纽约";公众人物(){}公共无效 DoSomething(){输入 t = city.GetType();string field_name = t.SomeUnkownFunction();//如果存在,将返回字符串city"!}}

有些人在下面的回答中问我为什么要这样做.这就是原因.

在我的现实世界中,城市上方有一个自定义属性.

[MyCustomAttribute("param1", "param2", etc)]公共字符串城市=纽约";

我在其他代码中需要这个属性.为了获取属性,我使用反射.在反射代码中,我需要输入字符串city"

MyCustomAttribute attr;类型 t = typeof(Person);foreach(t.GetFields() 中的 FieldInfo 字段){if (field.Name == "city"){//当我们找到具有我们需要的属性的字段时做一些事情}}

现在这不是类型安全的.如果我在 Person 的字段声明中将变量city"更改为workCity",除非我知道更新字符串,否则此行将失败

if (field.Name == "workCity")//我必须在另一个文件中进行此更改才能使其仍然有效,yuk!{}

所以我试图找到某种方法将字符串传递给这段代码,而无需实际输入它.

是的,我可以在 Person(或类似的东西)中将它声明为字符串常量,但仍然会输入两次.

呸!这很难解释!!

谢谢

感谢所有回答这个问题的人*很多*.它让我走上了一条更好地理解 lambda 表达式的新途径.它创造了一个新问题.

解决方案

也许你需要这个.工作正常.

我在这里找到了这个.

static void Main(string[] args){var domain = "矩阵";Check(() => domain);Console.ReadLine();}static void Check<T>(Expression<Func<T>> expr){var body = ((MemberExpression)expr.Body);Console.WriteLine("Name is: {0}", body.Member.Name);Console.WriteLine("值是:{0}", ((FieldInfo)body.Member).GetValue(((ConstantExpression)body.Expression).Value));}

输出将是:

<前>名称是:'域'值为:'矩阵'

Possible Duplicate:
Finding the Variable Name passed to a Function in C#

The class below contains the field city.

I need to dynamically determine the field's name as it is typed in the class declaration i.e. I need to get the string "city" from an instance of the object city.

I have tried to do this by examining its Type in DoSomething() but can't find it when examining the contents of the Type in the debugger.

Is it possible?

public class Person
{
  public string city = "New York";

  public Person()
  {
  }


  public void DoSomething()
  {
    Type t = city.GetType();

    string field_name = t.SomeUnkownFunction();
    //would return the string "city" if it existed!
  }
}

Some people in their answers below have asked me why I want to do this. Here's why.

In my real world situation, there is a custom attribute above city.

[MyCustomAttribute("param1", "param2", etc)]
public string city = "New York";

I need this attribute in other code. To get the attribute, I use reflection. And in the reflection code I need to type the string "city"

MyCustomAttribute attr;
Type t = typeof(Person);

foreach (FieldInfo field in t.GetFields())
{

  if (field.Name == "city")
  {
    //do stuff when we find the field that has the attribute we need
  }

}

Now this isn't type safe. If I changed the variable "city" to "workCity" in my field declaration in Person this line would fail unless I knew to update the string

if (field.Name == "workCity")
//I have to make this change in another file for this to still work, yuk!
{
}

So I am trying to find some way to pass the string to this code without physically typing it.

Yes, I could declare it as a string constant in Person (or something like that) but that would still be typing it twice.

Phew! That was tough to explain!!

Thanks

Thanks to all who answered this * a lot*. It sent me on a new path to better understand lambda expressions. And it created a new question.

解决方案

Maybe you need this. Works fine.

I found this here.

static void Main(string[] args)
{
    var domain = "matrix";
    Check(() => domain);
    Console.ReadLine();
}

static void Check<T>(Expression<Func<T>> expr)
{
    var body = ((MemberExpression)expr.Body);
    Console.WriteLine("Name is: {0}", body.Member.Name);
    Console.WriteLine("Value is: {0}", ((FieldInfo)body.Member)
   .GetValue(((ConstantExpression)body.Expression).Value));
}

Output will be:

Name is: 'domain'
Value is: 'matrix'

这篇关于你如何得到一个变量的名字,因为它是在它的声明中物理输入的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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