3

c language has macro like FILE/LINE to print file name and code line number

So how does C# language achieve same goal, is it possible to do, or require assembly packages?

Thanks a lot.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Troskyvs
  • 7,537
  • 7
  • 47
  • 115

1 Answers1

3

You can use StackFrame, i would be wary about doing this though, it will come at a performance cost and is likely to give you issues in release .

Represents a stack trace, which is an ordered collection of one or more stack frames.

var stackTrace = new StackTrace(0, true);
var sf = stackTrace.GetFrame(0);
Console.WriteLine("FileName: {0}", sf.GetFileName());
Console.WriteLine("Line Number:{0} ",sf.GetFileLineNumber());
Console.WriteLine("Function Name:{0}",sf.GetMethod ());
TheGeneral
  • 79,002
  • 9
  • 103
  • 141