访问使用包含.NET中的变量名的字符串的方法局部变量值 [英] Access a methods local variable value using a string containing the variable name in .Net

查看:222
本文介绍了访问使用包含.NET中的变量名的字符串的方法局部变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图得到一个基于包含变量名的字符串已宣布在本地方法的变量的值。我一直在尝试使用反射,张贴在多个 SO线程的。变量的名称被存储在一个表中,并以相同的方法来访问。



我有被看到方法局部变量或方法本身该问题物。我可以看到一个全局变量的值,如下面的代码片段显示,但我无法找到我的方法的局部变量。换句话说,该变量TestrecordType被声明为类变量,我可以在wusing下面的代码的任何方法访问它。我需要访问在其中代码住本地方法的变量。谁能帮我下深入到方法的水平?这可能吗?



  // 2013年7月10日DRowe收集数据的JSON类
Corporate_Record clsCorporateRecord =新Corporate_Record( );

//获取该类
ABC.Services.SYS.BmsBoardingEntryLayoutSpecificationCollection colLayoutSpecs =新ABC.Services.SYS.BmsBoardingEntryLayoutSpecificationCollection映射();
colLayoutSpecs = ABC.Services.SYS.BmsBoardingEntryLayoutSpecification.GetBySubClass(Corporate_Record);
的foreach(ABC.Services.SYS.BmsBoardingEntryLayoutSpecification SpecRecord在colLayoutSpecs){

//改变主类水平(全球)变量
TestrecordType = TestrecordType +现在;

myProperty的字符串= SpecRecord.FieldName;
串MYVARIABLE = SpecRecord.MapToCodeVariable;
字符串=的myType SpecRecord.Type; 。

//可以抓取Main类变量的值,但不是本地方法变量的值
VAR的结果= this.GetType()getfield命令(TestrecordType)的GetValue(本);

clsCorporateRecord.GetType()的getProperty(myProperty的).SetValue(clsCorporateRecord,结果,空)。

MethodInfo的明复= typeof运算(工人).GetMethod(CreateCorporateRecord01);
}
myCollection.Corporate_Record.Add(clsCorporateRecord);
//10.07.2013 DRowe END


解决方案

局部变量不反映代表。你可以看到全局,因为他们是一个类型的成员。当地居民,然而,当前的堆栈上只是插槽,该叠层不暴露于反射的API。



这调用堆栈编程暴露的唯一地方是在异常堆栈跟踪。你可以故意抛出一个异常,抓住它,并通过堆栈跟踪挑。不过,这将是一个坏,坏,想法,以及极其缓慢。


I am attempting to get the value of a variable that has been declared locally in a method based on a string containing the variables name. I have been trying to use reflection as posted in multiple SO threads. The name of the variable is being stored in a table and accessed in the same method.

The problem I am having is seeing the methods local variable or the Method itself for that matter. I can see that value of a global variable as indicated in the code snippet below but I can not find the local variable in my methods. In other words the the variable TestrecordType is declared as a class variable and I can access it in any method wusing the code below. I need to access the variables in the local method where the code lives. Can anyone help me drill down to the methods level? Is it possible?

//10.07.2013 DRowe Collecting data for the json class
Corporate_Record clsCorporateRecord = new Corporate_Record();

// get the mapping for the class
ABC.Services.SYS.BmsBoardingEntryLayoutSpecificationCollection colLayoutSpecs = new ABC.Services.SYS.BmsBoardingEntryLayoutSpecificationCollection();
colLayoutSpecs = ABC.Services.SYS.BmsBoardingEntryLayoutSpecification.GetBySubClass("Corporate_Record");
foreach (ABC.Services.SYS.BmsBoardingEntryLayoutSpecification SpecRecord in colLayoutSpecs) {

    // Alter main class level (Global) variable
    TestrecordType = TestrecordType + " Now";

    string myproperty = SpecRecord.FieldName ;
    string myVariable = SpecRecord.MapToCodeVariable;
    string myType = SpecRecord.Type;

    // Can grab Main class variable values but not the local method variable values
    var result = this.GetType().GetField("TestrecordType").GetValue(this);

    clsCorporateRecord.GetType().GetProperty(myproperty).SetValue(clsCorporateRecord, result, null);

    MethodInfo mInfo = typeof(Worker).GetMethod("CreateCorporateRecord01");                         
}                    
myCollection.Corporate_Record.Add(clsCorporateRecord);
//10.07.2013 DRowe END

解决方案

Local variables aren't representing in reflection. You can see globals because they're members of a type. Locals, however, are just slots on the current stack, and that stack isn't exposed to the reflection APIs.

The only place that the call stack is exposed programmatically is in the stack trace in an exception. You could throw an exception deliberately, catch it, and pick through the stack trace. But it would be a bad, bad, idea, and extremely slow.

这篇关于访问使用包含.NET中的变量名的字符串的方法局部变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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