Is there a C# equivalent to C++ __FILE__, __LINE__ and __FUNCTION__ macros?

c#

Solution

If you are using .net 4.5 you can use CallerMemberName CallerFilePath CallerLineNumber attributes to retrieve this values.

public void DoProcessing()
{
    TraceMessage("Something happened.");
}

public void TraceMessage(string message,
    [CallerMemberName] string memberName = "",
    [CallerFilePath] string sourceFilePath = "",
    [CallerLineNumber] int sourceLineNumber = 0)
{
    Trace.WriteLine("message: " + message);
    Trace.WriteLine("member name: " + memberName);
    Trace.WriteLine("source file path: " + sourceFilePath);
    Trace.WriteLine("source line number: " + sourceLineNumber);
}

If you are using older framework and visual 2012 you just need to declare them as they are in framework (same namespace) to make them work.

Problem

I have a C++ code that I'm trying to migrate to C#. Over there, on C++, i was using the following macros definition for debugging purposes. ``` #define CODE_LOCATION(FILE_NAME, LINE_NUM, FUNC_NAME) LINE_NUM, FILE_NAME, FUNC_NAME #define __CODE_LOCATION__ CODE_LOCATION(__FILE__, __LINE__, __FUNCTION__) ``` Are there similar constructs in C#? I know there are no macros in C#, but is there any other way to get the current file, line and function values during execution?

Original source

Related problems