Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get local variables through reflection? [closed]

Tags:

c#

c#-4.0

I have a method with at least 50 local variables. I need to select a variable from a string value. Is it possible to access a local variable with C# reflection?

Edit:

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
}
like image 458
Imran Qadir Baksh - Baloch Avatar asked Oct 24 '25 10:10

Imran Qadir Baksh - Baloch


1 Answers

Real answer, no.

After edited OP: Definite no. The names are 'discarded' at compile time

You can get at the IL instructions, though (Method.Body).

Use Cecil Decompiler to 'guess' what would be variables (no names, though). However, it will be generally hard to distinguish (reused) temps from variables and all syntactic sugar is lost

Also: fix your code like the other comments say :)

like image 188
sehe Avatar answered Oct 26 '25 00:10

sehe