Get the name of the current method from code

**** work on debug only ****

from  https://stackoverflow.com/questions/26292033/stacktrace-class-methods-not-working-in-release-mode

StackTrace information will be most informative with Debug build configurations. By default, Debug builds include debug symbols, while Release builds do not. The debug symbols contain most of the file, method name, line number, and column information used in constructing StackFrame and StackTrace objects.

Actually, Release mode optimize code and dose not have Program DataBase file(.pdb)

In Release mode

Property -> Build -> Define Debug Constant (Check it)

Property -> Build -> Optmize Code (UnCheck it)


from https://stackoverflow.com/questions/2652460/how-to-get-the-name-of-the-current-method-from-code 


public string GetCurrentMethod()
{
    var st = new StackTrace();
    var sf = st.GetFrame(1);

    return sf.GetMethod().Name;
}





    public enum MethordName

    {

        Null = 0,

        Methord1= 1,

        Methord2= 2,

        Methord3= 3,

    }


        public MethordName GetCurrentMethod()

        {

            MethordName name = MethordName.Null;

            var st = new StackTrace();

            var sf = st.GetFrame(1);

            string currentMethodName =  sf.GetMethod().Name;


            foreach (MethordName methordName in Enum.GetValues(typeof(MethordName)))  

            {

                if (currentMethodName == methordName.ToString())

                    name = methordName;

            }  

            return name;

        }

留言

熱門文章