How do I get the current line number?

2019-01-03 13:26发布

Here is an example of what I want to do:

 MessageBox.Show("Error line number "+CurrentLineNumber);

Current line number will be the line number in the source code of this piece of code.

How can I do that?

7条回答
在下西门庆
2楼-- · 2019-01-03 14:04

I prefer one liners so:

int lineNumber = (new System.Diagnostics.StackFrame(0, true)).GetFileLineNumber();
查看更多
Deceive 欺骗
3楼-- · 2019-01-03 14:05

In .NET 4.5 / C# 5, you can get the compiler to do this work for you, by writing a utility method that uses the new caller attributes:

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 + ")");
}

This will display, for example:

Boo at line 39 (SomeMethodSomewhere)

There's also [CallerFilePath] which tells you the path of the original code file.

查看更多
Animai°情兽
4楼-- · 2019-01-03 14:06

This works for me:

try
{
 //your code;
}
catch(Exception ex)
{
  MessageBox.Show(ex.StackTrace + " ---This is your line number, bro' :)", ex.Message);
}
查看更多
做个烂人
5楼-- · 2019-01-03 14:09

Use the StackFrame.GetFileLineNumber method, for example:

private static void ReportError(string message)
{
     StackFrame callStack = new StackFrame(1, true);
     MessageBox.Show("Error: " + message + ", File: " + callStack.GetFileName() 
          + ", Line: " + callStack.GetFileLineNumber());
}

See Scott Hanselman's Blog entry for more information.

[Edit: Added the following]

For those using .Net 4.5 or later, consider the CallerFilePath, CallerMethodName and CallerLineNumber attributes in the System.Runtime.CompilerServices namespace. For example:

public void TraceMessage(string message,
        [CallerMemberName] string callingMethod = string.Empty,
        [CallerFilePath] string callingFilePath = string.Empty,
        [CallerLineNumber] int callingFileLineNumber = 0)
{
    // Write out message
}

The arguments must be string for CallerMemberName and CallerFilePath and an int for CallerLineNumber and must have a default value. Specifying these attributes on method parameters instructs the compiler to insert the appropriate value in the calling code at compile time, meaning it works through obfuscation. See Caller Information for more information.

查看更多
Ridiculous、
6楼-- · 2019-01-03 14:16

For those who need a .NET 4.0+ method solution:

using System;
using System.IO;
using System.Diagnostics;

public static void Log(string message) {
   StackFrame stackFrame = new System.Diagnostics.StackTrace(1).GetFrame(1);
   string fileName = stackFrame.GetFileName();
   string methodName = stackFrame.GetMethod().ToString();
   int lineNumber = stackFrame.GetFileLineNumber();

   Console.WriteLine("{0}({1}:{2})\n{3}", methodName, Path.GetFileName(fileName), lineNumber, message);
}

How to call:

void Test() {
   Log("Look here!");
}

Output:

Void Test()(FILENAME.cs:104)

Look here!

Change the Console.WriteLine format how you like!

查看更多
干净又极端
7楼-- · 2019-01-03 14:16

If its in a try catch block use this.

try
{
    //Do something
}
catch (Exception ex)
{
    System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(ex, true);
    Console.WriteLine("Line: " + trace.GetFrame(0).GetFileLineNumber());
}
查看更多
登录 后发表回答