C# 顯示函數調用方的詳細信息

大體意思就是我有一個函數

FunctionA()
{
      ......
}

 

這個FunctionA函數在代碼出多次被調用,可能是在不同的文件、不同的位置,但是我在調試的時候,想知道到底是那一句代碼調用了FunctionA

就是這樣子。

那麼現在在.net我們可以使用Caller Information屬性。

 

特性說明類型
CallerFilePathAttribute 包含調用方的源文件的完整路徑。 這是編譯時的文件路徑。 String
CallerLineNumberAttribute 源文件中調用方法的行號。 Integer
CallerMemberNameAttribute 調用方的方法或屬性名稱。 請參閱本主題後面的成員名稱 String

 

public void FunctionA([CallerFilePath] string callerFilePath = "", [CallerLineNumber] long callerLineNumber = 0, [CallerMemberName] string callerMember= "")
{
    Debug.WriteLine("Caller File Path: {0}, Caller Line Number: {1}, Caller Member: {2}", callerFilePath,callerLineNumber,callerMember);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章