vb.net 获取调用方法的名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1421002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:25:45  来源:igfitidea点击:

getting the name of the calling method

vb.netreflection

提问by zeocrash

Basically i've got a web service that i'm trying to put some kind of usage logging in. To do this i've created a class with a logging method. I instatiate the class on the service and then call the logging method in each of the web methods.

基本上,我有一个 Web 服务,我正在尝试将某种用法登录。为此,我创建了一个带有日志记录方法的类。我在服务上建立类,然后在每个 Web 方法中调用日志记录方法。

I'm trying to find a way of getting the name of the method that has called the loggong method

我试图找到一种方法来获取调用 loggong 方法的方法的名称

Public sub tstmethod
log_object.log_method
end sub

in this case the returned name i'm looking for is "tstmethod"

在这种情况下,我要查找的返回名称是“tstmethod”

Everywhere i've seen has said to either use

我见过的每个地方都说要么使用

Dim stackframe As New Diagnostics.StackFrame
Return stackframe.GetMethod.Name.ToString

Or to use

或者使用

Dim stackframe As New Diagnostics.StackFrame
Return stackframe.GetMethod.DeclaringType.FullName

which i call inside the logging method

我在日志记录方法中调用

getmethod.name.tostring returns the name of the logging method

getmethod.name.tostring 返回日志记录方法的名称

getmethod.declaringtype.fullname returns the name of the logging class

getmethod.declaringtype.fullname 返回日志类的名称

no matter what i do i cannot find a way of getting the name of the method that called the logging method in this case "tstmethod"

无论我做什么,我都找不到一种方法来获取在这种情况下调用日志记录方法的方法的名称“tstmethod”

回答by Vinay Sajip

You need to instantiate the StackFramewith an appropriate constructor. For example,

您需要StackFrame使用适当的构造函数实例化。例如,

Dim stackframe As New Diagnostics.StackFrame(1)

will skip one stack frame (the logging method itself) and get the frame for the caller of that method.

将跳过一个堆栈帧(日志记录方法本身)并获取该方法调用者的帧。

That said - if at all possible, I'd strongly recommend using a standard logging mechanism such as log4net.

也就是说 - 如果可能的话,我强烈建议使用标准日志记录机制,例如log4net

回答by Er. ?ridy

I think the above statement can be made into one single line:

我认为上面的语句可以写成一行:

MsgBox((New System.Diagnostics.StackTrace).GetFrame(1).GetMethod.Name)

This will display a Message Box as with calling method name.

这将显示一个带有调用方法名称的消息框。

回答by Thomas Beck

VB.Net

VB.Net

System.Reflection.MethodBase.GetCurrentMethod.Name()

回答by Pete OHanlon

You should be able to get this from the StackFrame with the constructor telling it how many frames to go up the chain, so getting the parent would use 1 (the frame is zero based, so 0 is the current method):

您应该能够从 StackFrame 获取它,构造函数告诉它在链上有多少帧,因此获取父级将使用 1(该帧是基于零的,因此 0 是当前方法):

Dim stackTrace as New Diagnostics.StackFrame(1)
Return stackTrace.GetMethod.Name

回答by Sauron

I am using using this code for getting the calling method

我正在使用此代码获取调用方法

string methodName = new StackFrame( 1, true ).GetMethod().Name;
methodName = " [" + methodName + "]";
return methodName;

remember to add namespace Diagnostics

记得添加命名空间诊断

回答by Ramanathan RM

I add the calling form as the first parameter for all sub routines and functions like calledfunction(me,para1,para2......) so that i can identify the calling form and use its name or text in the called routine

我将调用表单添加为所有子例程和函数的第一个参数,例如 callfunction(me,para1,para2......) 以便我可以识别调用表单并在被调用例程中使用其名称或文本

回答by Migol

Do not try using StackFrame. It will work only in debug mode which is a killer for application speed.

不要尝试使用 StackFrame。它只能在调试模式下工作,这是应用程序速度的杀手。

There is no way you can get this info in Release mode, you should consider changing design and adding parameter.

在发布模式下您无法获得此信息,您应该考虑更改设计和添加参数。