是否可以通过反射获得局部变量?

use*_*567 2 c# c#-4.0

我有一个至少有50个局部变量的方法.我需要从字符串值中选择一个变量.是否可以使用C#反射访问局部变量?

编辑:

void A()
{
    var a1 = List<MyClass>();
    var a2 = List<MyClass>();
    var a3 = List<MyClass>();
    var a4 = List<MyClass>();
    var a5 = List<MyClass>();
    ........................
    ........................
    ........................
    // I have a string value found at runtime. The possible values of it are a1,a2,a3,a4,a5,.... I need to select one
}
Run Code Online (Sandbox Code Playgroud)

seh*_*ehe 7

真实答案,没有.

编辑完OP后:确定没有.名称在编译时被"丢弃"

您可以通过IL指令获取(Method.Body).

使用Cecil Decompiler来"猜测"变量是什么(尽管没有名字).然而,通常很难区分(重用)临时变量和所有语法糖都会丢失

另外:像其他评论一样修复你的代码:)

  • 等待.在问题结束后你是怎么偷偷摸摸的? (2认同)