Is there anything like __FILE__ and __LINE__ ?

  • Thread starter Thread starter wsq
  • Start date Start date
W

wsq

in C++ we can use __FILE__ and __LINE__ to help us locating the bug, is
there anything like these in C# ?

thanks,
wsq
 
Hello wsq,

Unfortunatelly .NET have no similiar macros.
To get this functionality u have to use StackTrace class

Smth like this
using System;
using System.Diagnostics ;


namespace getline
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
StackTrace st=new StackTrace (0,true);


StackFrame sf=new StackFrame ();
sf=st.GetFrame (0);
Console.WriteLine ("FileName: {0}",sf.GetFileName
());
Console.WriteLine ("Line Number:
{0}",sf.GetFileLineNumber ());
Console.WriteLine ("Function Name:
{0}",sf.GetMethod ());


Console.Read ();
}
}


}

w> in C++ we can use __FILE__ and __LINE__ to help us locating the bug,
w> is there anything like these in C# ?

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Hello wsq,
Unfortunatelly .NET have no similiar macros.
To get this functionality u have to use StackTrace class

Smth like this
using System; using System.Diagnostics ;

namespace getline { class Class1 {
[STAThread] static void Main(string[] args)
{ StackTrace st=new StackTrace
(0,true);

StackFrame sf=new StackFrame ();
sf=st.GetFrame (0);
Console.WriteLine ("FileName: {0}",sf.GetFileName ());
Console.WriteLine ("Line Number:
{0}",sf.GetFileLineNumber ());
Console.WriteLine ("Function Name: {0}",sf.GetMethod ());

Console.Read (); } }

}

w> in C++ we can use __FILE__ and __LINE__ to help us locating the bug,
w> is there anything like these in C# ?

---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do
not cease to be insipid." (c) Friedrich Nietzsche

Thank you, Michael, it works, but only in debug version. In release
version, GetFileName() always return zero. Am I missing something?

regards,
wsq
 
No, you're not. Release assemblies don't contain enough information to
relate a stack trace back to the precise source file name and line
number.

The only reason that this worked in C / C++ is that these were macros
that were resolved at compile time. C# resolves its file name and line
number information at run time, and so depends upon symbolic
information available (or not) at run time.
 
Back
Top