Is there compile-time access to line numbers in C#?

c#

Solution

No, C# doesn't have a macro preprocessor or any meta programming features, so there are no "Compile time" solutions in the entire language. But there are 3rd party macro languages out there that you can use, if you have to, but of course it complicates the build process, Visual Studio won't just figure out how to built it by itself.

You can even use the C preprocessor if you want it. (assuming MSVC compiler)

cl.exe /TC /P /C /EP something.cs > something.raw.cs

- `cl.exe` is the C compiler

- `/TC` tells the C compiler to treat all files as C sources despite their extensions

- `/P` tells the C compiler to only preprocess the file do not compile it

- `/C` preserves the comments

- `/EP` prevents the compiler from generating #line directives, that the C# compiler wouldn't understand

This will allow you to use `#include`, `#define` and `#if` as well as `__FILE__` and `__LINE__` in your C# program, but again you have to set up Visual Studio to do this additional compilation step, or use a different build system.

Problem

I'm writing a C# program using Visual Studio 2010 where I want to write out certain events to a log file and include the line number the code was on when that happened. I've only found two ways of capturing line numbers - CallerLineNumber, which requires .Net 4.5/C#5 (I'm targeting .Net 4) and StackFrame.GetFileLineNumber, which apparently requires a debug build and pdb file to work properly, and I'm producing a release build and no pdb file. But here's what I don't get - both of the above are run-time solutions, but line numbers are compile-time entities. Why is a runtime solution necessary? I could type in the correct line number as a literal constant by just looking at the bottom of the screen where it says something like "ln 175" . . . ``` LogEvent("It happened at line 175"); ``` but the problem with that is that if I edit any code before line 175 my literal might no longer be correct. But the compiler knows the correct line number and I've used programming languages in the past that could just pop in the correct line number as a compile time constant. (e.g., ANSI C and Microsoft C++ support a predefined macro called `_LINE_`) Is there any way to get C# to do that? If not are there any solutions to my problem?

Original source

Related problems