How to get the source file name and the line number of a type member?

后端 未结 3 1319
日久生厌
日久生厌 2020-12-03 13:50

Considering that the debug data file is available (PDB) and by using either System.Reflection or another similar framework such as Mono.Cecil

3条回答
  •  时光说笑
    2020-12-03 14:21

    Up to date method:

    private static void Log(string text,
                            [CallerFilePath] string file = "",
                            [CallerMemberName] string member = "",
                            [CallerLineNumber] int line = 0)
    {
        Console.WriteLine("{0}_{1}({2}): {3}", Path.GetFileName(file), member, line, text);
    }
    

    New Framework API which populates arguments (marked with special attributes) at runtime, see more in my answer to this SO question

提交回复
热议问题