Print the source filename and linenumber in C#

后端 未结 5 1911
盖世英雄少女心
盖世英雄少女心 2020-11-29 23:24

Is there any way to retrieve the current source filename and linenumber in C# code and print that value in the console output? Like LINE and FILE

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 23:51

    This answer is outdated! See @taras' answer for more recent information.


    No constant :(

    What you can do is a lot uglier :

    string currentFile = new System.Diagnostics.StackTrace(true).GetFrame(0).GetFileName(); 
    int currentLine = new System.Diagnostics.StackTrace(true).GetFrame(0).GetFileLineNumber(); 
    

    Works only when PDB files are available.

提交回复
热议问题