How do I use Reflection to get the fields of a method?

415 Views Asked by At

How can I user reflection to get the fields inside a method? I tried to use the MethodInfo and then GetFields but the values returned are not the fields I defined within the method.

For example, using the code below, how would I get the FieldInfo for LineNo?

    private void MyMethod()
    {
        int LineNo = 0;
    }

Thanks

2

There are 2 best solutions below

0
On
  1. These are not fields but local variables.
  2. At best you can get IL of method body. If you can somehow parse that you should be able to get your hands at all the locals declared in the method.
1
On

With reflection you can't get values of field in code of function,

You can get the values of field of Class/Object.