Reflection Get Variables in Method

后端 未结 2 755
逝去的感伤
逝去的感伤 2020-12-19 10:04

How can i get varibles used in Method which then I will write their values on Console ?

2条回答
  •  执念已碎
    2020-12-19 11:08

    I don think its possible, but if you dig IL code and look at the Method.Body. You can know about the temporary, local variables used.

    But it will be difficult to differentiate temps from variables cos all the syntactic sugar is gone

    UPDATE: Jus while searching on this question found it. Not sure if it works.

    System.Diagnostics.StackFrame stackFrame = new System.Diagnostics.StackFrame();
     System.Reflection.MethodBase methodBase = stackFrame.GetMethod();
    
     methodBase.GetParameters(); //Array of System.Reflection.ParameterInfo[]
     methodBase.GetMethodBody().LocalVariables; //List of Local variables declared in the body
    

提交回复
热议问题