lundi 28 novembre 2016

Get the current name and value of a variable inside the method with Reflection - C#

I'm trying to get all local variables with names and current values when an error occured in the method. I searched for it and what I found is I must use Reflection for this puspose but I couldn't be successfull.

Below is my code, in the catch block I want to get all locals into a string named "allVariables". Is it possible to get these with names & values ?

namespace AUBank.Credit
{
    public class CreditEval : MarshalByRefObject
    {       
    ... 
    public DataSet GetCreditEvalbyRef(string cRef)
    {           

        string allVariables = "";

        try
        {
            int a;
            double b;
            string c;   

            /*
            other code
            */                              
        }
        catch (Exception)
        {
            MethodBase method = (MethodBase)this.GetType().GetMember("GetCreditEvalbyRef")[2];
            MethodBody body = method.GetMethodBody();

            foreach (LocalVariableInfo variableInfo in body.LocalVariables) 
            {
                allVariables += "Variable: " + variableInfo.ToString() + "\n";
            }       
        }
    }
    }   
}

Thank you.





Aucun commentaire:

Enregistrer un commentaire