C#在接收方法中获取调用方法,行号,类文件和方法名称等,而无需解析任何变量或使用引用?

发布于 2025-01-29 03:44:09 字数 2829 浏览 2 评论 0 原文

(已解决) 我有自己的类库,我在编程课程中已连续开发,迭代和修改,其中包含我在常规上使用的多种方法。例如,我有一种使用 class b_core.writedebug(string); 使用 class 从任何 class 调用的方法。 当前,此方法仅采用我编写的任何文本,并且允许我使用我自己的 library 在类中不包含 system.diarostics

这可以偶尔会导致我只是想知道一个数字的情况,这可能是 b_core.writedebug(integer.tostring()); 。由于我是作为程序员开发的,因此我确实尝试始终包含A 参考点,但是当我错过它时,有时我只是在 debug中获得 line 输出说“ 4”或类似的话。

因此,我目前正在尝试对此方法进行体面的更正。目前,我唯一的想法是将stacktrace解析为该方法。

public static void WriteDebug(string text, StackTrace trace) {
    var frame = trace.GetFrame(0);
    string writeString = "Line: " + 
                            frame.GetFileLineNumber() + " | " + 
                            frame.GetFileName() + " | " + 
                            frame.GetMethod() + " | Text:\n";
    writeString += text;

    // Debug output => Line: x | File: Program.cs | Main | Text:
    //                 Hello world!
    Debug.WriteLine(writeString);
}

这将起作用,但是它要求我使用System.diagnostics 添加 将要使用 b_core.writeline()。这可能会导致一些毫无意义的书面添加,这也是 class 授予 debug.writeline()的易于访问性,这删除了我为此使用自己的方法的部分原因在库中

这也需要我实际解析stacktrace,当我只是想知道一个简单的事情以进行故障排除或类似的事情时,添加了更多要编写的内容。

理想的情况将是:

public static void WriteDebug(string text) { //Removed the StackTrace parse
    var frame = Caller.trace.GetFrame(0);    //Added a hypothetical Caller object
    string writeString = "Line: " + 
                            frame.GetFileLineNumber() + " | " + 
                            frame.GetFileName() + " | " + 
                            frame.GetMethod() + " | Text:\n";
    writeString += text;

    // Debug output => Line: x | File: Program.cs | Main | Text:
    //                 Hello world!
    Debug.WriteLine(writeString);
}

我只需引用调用方法本身就可以获取数据。因此,我的问题是有什么方法可以在不添加 呼叫方法的类别上的额外参考的情况下做任何事情,并希望不必将任何其他对象放在方法

感谢您对此的任何见解。

此致!

解决方案,感谢Fildor在评论中。维克多的答案也很好,可能更接近实际问题。但是我决定使用Fildor的建议。

这是目前的方法:

public static void WriteTrace(string text, bool printTrace = true,
    [System.Runtime.CompilerServices.CallerMemberName] string membName = "",
    [System.Runtime.CompilerServices.CallerFilePath] string filePath = "",
    [System.Runtime.CompilerServices.CallerLineNumber] int lineNumber = 0) {

            
    if (printTrace) 
        Trace.Write("Line: " + 
            lineNumber + " | " + 
            filePath.Split('\\').Last() + " | " + 
            membName + ":\t"
        );
    Trace.WriteLine(text);
    //Output => Line: 208 | BMenu.cs | MoveSelection:   0
}

(SOLVED)
I have my own class library that I've been successively developed, iterated and modified during my programming course, it contains a multitude of methods that I use on the regular. For example I have a method that is called from any class using the class with B_Core.WriteDebug(string);.
This method currently only takes whatever text I've written, and allows me to not include the System.Diagnostics in the classes using my own library.

This can, on occassion lead to situations where I simply want to know a single digit, and it could be something like B_Core.WriteDebug(integer.ToString());. As I've developed as a programmer I do try to always include a reference point, but when I miss it, I sometimes just get a line in the Debug output saying "4" or something along those lines.

So, I am currently trying to make a decent correction to this method. Currently the only idea I have is to parse a StackTrace to the method.

public static void WriteDebug(string text, StackTrace trace) {
    var frame = trace.GetFrame(0);
    string writeString = "Line: " + 
                            frame.GetFileLineNumber() + " | " + 
                            frame.GetFileName() + " | " + 
                            frame.GetMethod() + " | Text:\n";
    writeString += text;

    // Debug output => Line: x | File: Program.cs | Main | Text:
    //                 Hello world!
    Debug.WriteLine(writeString);
}

This'll work, but it requires me to add the using System.Diagnostics to each class that is gonna use the B_Core.WriteLine(). This can result in some pointless written additions, it's also the class granting easy accessibility to Debug.WriteLine(), which removes part of my reason for having my own method for this in the library.

It also requires me to actually parse the StackTrace, adding more things to write when I simply want to know a simple thing for troubleshooting or similar.

The ideal scenario would be something like:

public static void WriteDebug(string text) { //Removed the StackTrace parse
    var frame = Caller.trace.GetFrame(0);    //Added a hypothetical Caller object
    string writeString = "Line: " + 
                            frame.GetFileLineNumber() + " | " + 
                            frame.GetFileName() + " | " + 
                            frame.GetMethod() + " | Text:\n";
    writeString += text;

    // Debug output => Line: x | File: Program.cs | Main | Text:
    //                 Hello world!
    Debug.WriteLine(writeString);
}

Where I'd get the data by just referencing the calling method itself. So my question is there any way to do something like this without adding extra references atop the calling method's class as well as hopefully not having to parse any other object to the method?

Thanks for any insight into this.

Best regards!

Solution, thanks to Fildor in the comments. The answer by Victor is also good and might be closer to the actual question. But I've decided to use Fildor's recommendation.

This is the method at this time:

public static void WriteTrace(string text, bool printTrace = true,
    [System.Runtime.CompilerServices.CallerMemberName] string membName = "",
    [System.Runtime.CompilerServices.CallerFilePath] string filePath = "",
    [System.Runtime.CompilerServices.CallerLineNumber] int lineNumber = 0) {

            
    if (printTrace) 
        Trace.Write("Line: " + 
            lineNumber + " | " + 
            filePath.Split('\\').Last() + " | " + 
            membName + ":\t"
        );
    Trace.WriteLine(text);
    //Output => Line: 208 | BMenu.cs | MoveSelection:   0
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夕色琉璃 2025-02-05 03:44:09

您可以使用属性:

functionName来实现此目的: callermemebernameattribute

呼叫者文件: callerfilepathattribute

caller行号: callerlinenumberattribute

You can achieve this by using Attributes:

FunctionName that called: CallerMemeberNameAttribute

Caller File: CallerFilePathAttribute

Caller Line Number: CallerLineNumberAttribute

予囚 2025-02-05 03:44:09

您可以使假设的呼叫者对象在堆栈上上升:

public static void WriteDebug(string text)
{
    var stackTrace = new StackTrace();
    var frame = stackTrace.GetFrame(stackTrace.FrameCount - 2);

    string writeString = "Línea: " +
        frame.GetFileLineNumber() + " | " +
        frame.GetFileName() + " | " +
        frame.GetMethod() + " | Text:\n";
    writeString += text;

    // Salida de depuración => Línea: x | Archivo: Program.cs | Principal | Texto:
    // ¡Hola mundo!
    Debug.WriteLine(writeString);
}

stacktrace.getFrame(stacktrace.framecount -1)是最后一个帧,您的 Witterebug method。使用上一个帧,您正在使用呼叫者方法。

You can get your hypothetical Caller object going up on the stack:

public static void WriteDebug(string text)
{
    var stackTrace = new StackTrace();
    var frame = stackTrace.GetFrame(stackTrace.FrameCount - 2);

    string writeString = "Línea: " +
        frame.GetFileLineNumber() + " | " +
        frame.GetFileName() + " | " +
        frame.GetMethod() + " | Text:\n";
    writeString += text;

    // Salida de depuración => Línea: x | Archivo: Program.cs | Principal | Texto:
    // ¡Hola mundo!
    Debug.WriteLine(writeString);
}

stackTrace.GetFrame(stackTrace.FrameCount - 1) is the last frame, your WriteDebug method. Using the previous frame you are using the caller method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文