Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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?

like image 888
NirMH Avatar asked Nov 14 '12 11:11

NirMH


People also ask

Why is there a in AC?

What A/C Means. The term “A/C” stands for “air conditioning,” but it's frequently used to describe any type of home cooling equipment, such as a traditional split-system air conditioner or heat pump, mini-split unit, geothermal system, or even a window unit.

Which is correct AC or AC?

Senior Member. A/C unit (air conditioning unit) is a single machine. (e.g. What's that ugly box on your wall? - It's the air conditioning unit.) A/C (air conditioning) is the entire system, or the result it gives.

Why is it called AC?

He combined moisture with ventilation to "condition" and change the air in the factories, controlling the humidity so necessary in textile plants. Willis Carrier adopted the term and incorporated it into the name of his company. Domestic air conditioning soon took off.

What means AC?

a/ c is an abbreviation for air-conditioning. Keep your windows up and the a/c on high.


1 Answers

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.

like image 188
Rafal Avatar answered Sep 29 '22 06:09

Rafal