获取调用方法的类名. [英] Get the class name of a calling method.

查看:87
本文介绍了获取调用方法的类名.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建过程日志时,我需要知道何时写入日志,因此我需要调用方法的名称,当然,还需要拥有该方法的类的名称.方法的名称是使用反射完成的,但是无法获取存储方法的类名称.

这是示例代码:

When creating a process log, I need to know when the log is written, so I need the name of the calling method, and of course, the name of the class who owns that method. The name of the method is done using reflection, but there is no way to get the class name where the method is stored.

This is the sample code :

using System.Windows.Forms;
using System.Reflection;
using System.Diagnostics;

class LogCaller {
    public static void callLogProcess() {
        viewLog("Any log");
    }
    public static void viewLog(string logStatus) {
        StackTrace st = new StackTrace();
        string str;
        str = "Frame Count : " + st.FrameCount.ToString() + "\n";
        int i = 0;
        foreach (StackFrame sf in st.GetFrames()) {
            MethodBase mb = sf.GetMethod();
            str += i.ToString() + "." + mb.Name + " in " + mb.Module.Name + "\n";
            i++;
        }
        MessageBox.Show(str);
    }
}



然后,我在按钮的单击事件中调用LogCaller.callLogProcess(),这就是结果:

帧数:25
0.view登录Ai.Trade.exe
1. Ai.Trade.exe中的callLogProcess
2.button1_单击Ai.Trade.exe中的
3.在System.Windows.Forms.dll中单击
4.在System.Windows.Forms.dll中单击
5.System.Windows.Forms.dll中的OnMouseUp
6. System.Windows.Forms.dll中的WmMouseUp
7.System.Windows.Forms.dll中的WndProc
8.System.Windows.Forms.dll中的WndProc
9.System.Windows.Forms.dll中的WndProc
10.System.Windows.Forms.dll中的OnMessage
11.System.Windows.Forms.dll中的WndProc 12.System.Windows.Forms.dll中的debuggablecallback
13.System.Windows.Forms.dll中的DispatchMessageW
14.System.Windows.Forms.dll中的System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop
15.System.Windows.Forms.dll中的RunMessageLoopInner
16.System.Windows.Forms.dll中的RunMessageLoop
17.在System.Windows.Forms.dll中运行
18.Ai.Trade.exe中的主要组件
mscorlib.dll中的19._nExecuteAssembly
20.mscorlib.dll中的ExecuteAssembly
21.Microsoft.VisualStudio.HostingProcess.Utilities.dll中的RunUsersAssembly
22.mscorlib.dll中的ThreadStart_Context
23.在mscorlib.dll中运行
24.mscorlib.dll中的ThreadStart

如何获得拥有"callLogProcess"方法的类名(在这种情况下为LogCaller),有人可以帮助我吗?



Then I call the LogCaller.callLogProcess() in a click event of a button, and this is the result :

Frame Count : 25
0.viewLog in Ai.Trade.exe
1.callLogProcess in Ai.Trade.exe
2.button1_Click in Ai.Trade.exe
3.OnClick in System.Windows.Forms.dll
4.OnClick in System.Windows.Forms.dll
5.OnMouseUp in System.Windows.Forms.dll
6.WmMouseUp in System.Windows.Forms.dll
7.WndProc in System.Windows.Forms.dll
8.WndProc in System.Windows.Forms.dll
9.WndProc in System.Windows.Forms.dll
10.OnMessage in System.Windows.Forms.dll
11.WndProc in System.Windows.Forms.dll
12.DebuggableCallback in System.Windows.Forms.dll
13.DispatchMessageW in System.Windows.Forms.dll
14.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop in System.Windows.Forms.dll
15.RunMessageLoopInner in System.Windows.Forms.dll
16.RunMessageLoop in System.Windows.Forms.dll
17.Run in System.Windows.Forms.dll
18.Main in Ai.Trade.exe
19._nExecuteAssembly in mscorlib.dll
20.ExecuteAssembly in mscorlib.dll
21.RunUsersAssembly in Microsoft.VisualStudio.HostingProcess.Utilities.dll
22.ThreadStart_Context in mscorlib.dll
23.Run in mscorlib.dll
24.ThreadStart in mscorlib.dll

How to get the class name who owns the "callLogProcess" method (LogCaller in this case), can anyone help me ?

推荐答案


您可以尝试使用以下代码:

Hi,
you can try using the below code:

StackFrame frame = new StackFrame(1);
//class name
string className = frame.GetMethod().DeclaringType.Name;
//method name
string methodName = frame.GetMethod().Name;



问候



Regards


这篇关于获取调用方法的类名.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆