C# 从方法中检索调用方法名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/615940/
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
Retrieving the calling method name from within a method
提问by Scott Vercuski
Possible Duplicate:
How can I find the method that called the current method?
可能的重复:
如何找到调用当前方法的方法?
I have a method in an object that is called from a number of places within the object. Is there a quick and easy way to get the name of the method that called this popular method.
我在对象中有一个方法,该方法从对象内的多个位置调用。是否有一种快速简便的方法来获取调用此流行方法的方法的名称。
Pseudo Code EXAMPLE:
伪代码示例:
public Main()
{
PopularMethod();
}
public ButtonClick(object sender, EventArgs e)
{
PopularMethod();
}
public Button2Click(object sender, EventArgs e)
{
PopularMethod();
}
public void PopularMethod()
{
//Get calling method name
}
Within PopularMethod()
I would like to see the value of Main
if it was called from Main
... I'd like to see "ButtonClick
" if PopularMethod()
was called from ButtonClick
在里面PopularMethod()
我想看看Main
它是否被调用的价值Main
......我想看到“ ButtonClick
”如果PopularMethod()
被调用ButtonClick
I was looking at the System.Reflection.MethodBase.GetCurrentMethod()
but that won't get me the calling method. I've looked at the StackTrace
class but I really didn't relish running an entire stack trace every time that method is called.
我正在查看,System.Reflection.MethodBase.GetCurrentMethod()
但这不会让我获得调用方法。我看过这个StackTrace
类,但我真的不喜欢每次调用该方法时都运行整个堆栈跟踪。
采纳答案by jason
I don't think it can be done without tracing the stack. However, it's fairly simple to do that:
我认为不跟踪堆栈就无法完成。但是,这样做相当简单:
StackTrace stackTrace = new StackTrace();
MethodBase methodBase = stackTrace.GetFrame(1).GetMethod();
Console.WriteLine(methodBase.Name); // e.g.
However, I think you really have to stop and ask yourself if this is necessary.
但是,我认为您真的必须停下来问问自己是否有必要这样做。
回答by John Leidegren
This is actually really simple.
这其实很简单。
public void PopularMethod()
{
var currentMethod = System.Reflection.MethodInfo
.GetCurrentMethod(); // as MethodBase
}
But be careful through, I'm a bit skeptical to if inlining the method has any effect. You can do this to make sure that the JIT compiler won't get in the way.
但是要小心,我有点怀疑内联该方法是否有任何影响。您可以这样做以确保 JIT 编译器不会妨碍您。
[System.Runtime.CompilerServices.MethodImpl(
System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
public void PopularMethod()
{
var currentMethod = System.Reflection.MethodInfo
.GetCurrentMethod();
}
To get the calling method:
获取调用方法:
[System.Runtime.CompilerServices.MethodImpl(
System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
public void PopularMethod()
{
// 1 == skip frames, false = no file info
var callingMethod = new System.Diagnostics.StackTrace(1, false)
.GetFrame(0).GetMethod();
}
回答by Sruly
Just pass in a parameter
只需传入一个参数
public void PopularMethod(object sender)
{
}
IMO: If it's good enough for events it should be good enough for this.
IMO:如果它对事件来说足够好,那么它应该足够好。
回答by C. Dragon 76
I think you do need to use the StackTrace
class and then StackFrame.GetMethod()
on the next frame.
我认为您确实需要使用StackTrace
该类,然后再StackFrame.GetMethod()
使用下一帧。
This seems like a strange thing to use Reflection
for though. If you are defining PopularMethod
, can't go define a parameter or something to pass the information you really want. (Or put in on a base class or something...)
不过,这似乎是一件奇怪的事情Reflection
。如果您正在定义PopularMethod
,则无法定义参数或其他东西来传递您真正想要的信息。(或放入基类或其他东西......)
回答by BFree
While you can most definitley trace the Stack and figure it out that way, I would urge you to rethink your design. If your method needs to know about some sort of "state", I would say just create an enum or something, and take that as a Parameter to your PopularMethod(). Something along those lines. Based on what you're posting, tracing the stack would be overkill IMO.
虽然您可以最明确地跟踪堆栈并通过这种方式弄清楚,但我建议您重新考虑您的设计。如果您的方法需要了解某种“状态”,我会说只需创建一个枚举或其他东西,并将其作为您的 PopularMethod() 的参数。沿着这些路线的东西。根据您发布的内容,跟踪堆栈对于 IMO 来说太过分了。
回答by JonPen
I have often found my self wanting to do this, but have always ending up refactoring the design of my system so I don't get this "Tail wagging the dog" anti-pattern. The result has always been a more robust architecture.
我经常发现我自己想要这样做,但最终总是重构我的系统设计,所以我没有得到这种“尾巴摇狗”的反模式。结果始终是一个更健壮的架构。
回答by Marc Gravell
In .NET 4.5 / C# 5, this is simple:
在 .NET 4.5 / C# 5 中,这很简单:
public void PopularMethod([CallerMemberName] string caller = null)
{
// look at caller
}
The compileradds the caller's name automatically; so:
该编译器自动将来电者的姓名; 所以:
void Foo() {
PopularMethod();
}
will pass in "Foo"
.
将通过"Foo"
。