Retrieving Current Line Number in Your Code
If you're working with code and want to know which line number is currently being executed, there's a solution for you.
You can accomplish this using the compiler's assistance in .NET 4.5 / C# 5. Here's how:
using System.Runtime.CompilerServices; static void SomeMethodSomewhere() { ShowMessage("Boo"); }
static void ShowMessage(string message, [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string caller = null) { MessageBox.Show(message + " at line " + lineNumber + " (" + caller + ")"); }
SomeMethodSomewhere();
The output will display, for instance:
Boo at line 39 (SomeMethodSomewhere)
Additionally, if you need the path of the original code file, the [CallerFilePath] attribute can provide that information.
The above is the detailed content of How Can I Get the Current Line Number in My C# Code?. For more information, please follow other related articles on the PHP Chinese website!