动态引用变量 [英] Reference variable dynamically

查看:61
本文介绍了动态引用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以动态引用变量?例如,


string a;


this.controls(" a")=" foobar";

解决方案

嗨Roger,

如果你想引用这样的变量那么你可以使用反射。

这允许您通过与类型相关联的元数据

获取有关类型的信息。例如,在下面的代码中,我通过传递变量的名称,通过反射设置名称字段

的值:


using System;

使用System.Collections;

使用System.Text;

使用System.Reflection;


名称空间ConsoleApplication4

{

class program

{

static void Main(string [] args)

{

人p =新人();

p.SetName(" bob");

}

}


班级人物

{

私人字符串名称;


public void SetName(string newName)

{

this.GetType()。GetField(" name",

BindingFlags .NonPublic | BindingFlags.Instance)。SetValue(this,newName);

}

}

}


希望有所帮助

Mark Dawson
http:/ /www.markdawson.org

" Roger"写道:

是否可以动态引用变量?例如,

字符串a;

this.controls(" a")=" foobar";



>如果你想引用这样的变量,你可以使用反射。


不,实际上,你不能,因为局部变量名称不可用

通过Reflection,反过来因为局部变量不保证在运行时甚至存在
。它们可能会被编译器优化掉。


您可以通过反射获取类型信息,但不能获取本地

变量的名称。

。你可以这样做,因为Control类有一个Name字段,

你可以用给定的名字搜索Control。同样,你

可以创建知道的类。他们自己的钥匙然后编写其他

类来搜索它们。


然而,没有(保证永远工作)的方式来检查代码

并找到局部变量名称并根据这些名称对它们进行操作。


Roger,

马克和布鲁斯都在这里指出了更好的问题。但我认为真正的

问题是,目标是什么?如果您可以提供更多关于您需要执行此类

操作的业务逻辑方案的信息,那么您可以获得更具体的建议。 to

first base。

Peter


-

联合创始人, Eggheadcafe.com开发者门户网站:
http://www.eggheadcafe.com

UnBlog:
http://petesbloggerama.blogspot.com


" Roger"写道:

是否可以动态引用变量?例如,

字符串a;

this.controls(" a")=" foobar";



Is it possible to reference a variable dynamically? For instance,

string a;

this.controls("a") = "foobar";

解决方案

Hi Roger,
if you want to reference variables like this then you can use reflection.
This allows you to obtain information about types through the metadata
associated with the type. For example, in the code below I set the value of
the name field through reflection by passing the name of the variable:

using System;
using System.Collections;
using System.Text;
using System.Reflection;

namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
Person p = new Person();
p.SetName("bob");
}
}

class Person
{
private string name;

public void SetName(string newName)
{
this.GetType().GetField("name",
BindingFlags.NonPublic | BindingFlags.Instance).SetValue(this, newName);
}
}
}

Hope that helps
Mark Dawson
http://www.markdawson.org
"Roger" wrote:

Is it possible to reference a variable dynamically? For instance,

string a;

this.controls("a") = "foobar";



> if you want to reference variables like this then you can use reflection.

No, actually, you can''t, because local variable names aren''t available
via Reflection, in turn because local variables aren''t guaranteed to
even exist at run time. They may be optimized away by the compiler.

You can get type information via reflection, but not names of local
variables.

However, Roger''s original post implied that he wants to find a Control
named "a". That you can do because the Control class has a Name field,
and you can search for the Control with the given Name. Similarly, you
can create classes that "know" their own "keys" and then write other
classes that search for them.

However, there is no (guaranteed-to-always-work) way to inspect code
and find local variable names and manipulate them based on those names.


Roger,
Mark and Bruce both pointed out the finer issues here. But I think the real
question here is, "what is the goal"? If you could supply more information
about the business logic scenario in which you need to perform this type of
operation, you can probably get more concrete suggestions on a way to get to
"frst base".
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Roger" wrote:

Is it possible to reference a variable dynamically? For instance,

string a;

this.controls("a") = "foobar";



这篇关于动态引用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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