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
}
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With