小编Dan*_*owe的帖子

使用包含.Net中变量名称的字符串访问方法局部变量值

我试图获取已在基于包含变量名称的字符串的方法中本地声明的变量的值.我一直在尝试使用多个SO线程中发布的反射.变量的名称存储在表中,并以相同的方法访问.

我遇到的问题是看到方法局部变量或方法本身.我可以看到下面的代码片段中指出的全局变量的值,但我在我的方法中找不到局部变量.换句话说,变量TestrecordType被声明为类变量,我可以使用下面的代码以任何方法访问它.我需要访问代码所在的本地方法中的变量.任何人都可以帮助我深入到方法级别吗?可能吗?

//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 …
Run Code Online (Sandbox Code Playgroud)

.net c# reflection

2
推荐指数
1
解决办法
1204
查看次数

标签 统计

.net ×1

c# ×1

reflection ×1