1

私は、メソッドとそのアクションのログを出力しようとしています。

Record#______MethodName__________MethodsOutput

1      ______GetCurrentTime______success(21:10)
2      ______DoSplitString_______faild(not in right Format)
3......................................etc'...

これは、1500行を超えるコードであり、少なくとも私にとっては少し複雑になりすぎているため、プログラムのデバッグに役立ちます。問題は、メソッド名を文字列に格納する正しい方法は何かです

public void MyMethod()
{
      do stuff 

       if (comleted) MyAppLog_ListView_AddRecord(string for the methods name, outputSucces)
       else if (faild) MyAppLog_ListView_AddRecord(string for the methods name, outputFail)


}

MyMethod().Name を文字列に格納するにはどうすればよいですか?

  • 再編集:

    SidAhmedsの回答について:

     public void MyMethodName()
    
        {
              do stuff 
    
             string Mnam = MethodBase.GetCurrentMethod().Name;
               if (comleted) MyAppLog_ListView_AddRecord(Mnam , outputSucces);
               else if (faild) MyAppLog_ListView_AddRecord(Mnam , outputFail);
    
    
        }
    
4

2 に答える 2

2

または、リフレクションを使用してそれを行うことができます:

System.Reflection.MethodBase.GetCurrentMethod().Name;
于 2012-08-28T07:03:25.137 に答える
1
StackTrace st = new StackTrace();
var methodName = st.GetFrame(0).GetMethod().Name;
于 2012-08-28T07:02:45.013 に答える