从被调用函数中获取调用函数名 [英] Get the calling function name from the called function

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

问题描述

可能的重复:
如何找到调用当前方法的方法方法?

如何从 C# 中的被调用函数中获取调用函数名称?

How can I get the calling function name from the called function in C#?

推荐答案

new StackFrame(1, true).GetMethod().Name

请注意,在发布版本中,编译器可能会内联被调用的方法,在这种情况下,上述代码将返回调用者的调用者,因此为了安全起见,您应该使用以下内容装饰您的方法:

Note that in release builds the compiler might inline the method being called, in which case the above code would return the caller of the caller, so to be safe you should decorate your method with:

[MethodImpl(MethodImplOptions.NoInlining)]

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

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