C# 方法调用者

标签 c# .net

<分区>

Possible Duplicate:
How can I find the method that called the current method?

你好, 我如何从方法中确定方法的调用者?例如:

SomeNamespace.SomeClass.SomeMethod() {
   OtherClass();
}

OtherClass() {
   // Here I would like to able to know that the caller is SomeNamespace.SomeClass.SomeMethod
}

谢谢

最佳答案

这些文章应该有帮助:

  1. http://iridescence.no/post/GettingtheCurrentStackTrace.aspx
  2. http://blogs.msdn.com/jmstall/archive/2005/03/20/399287.aspx

基本上代码是这样的:

StackFrame frame = new StackFrame(1);
MethodBase method = frame.GetMethod();
message = String.Format("{0}.{1} : {2}",
method.DeclaringType.FullName, method.Name, message);
Console.WriteLine(message);

关于C# 方法调用者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1163435/

相关文章:

c# - 如何使用服务器模式在 GridView 中获取异常详细信息?

.net - 在asp.net中使用if与eval ("")

.net - 根据 SQL 表时间戳安排 .exe 执行

.net - Nuget:将多个大型解决方案迁移到 PackageReference

c# - UserControl 中的依赖属性绑定(bind)

c# - Picturebox 中的可移动 FrameControl 无法按预期工作

c# - 如何返回以特定字符开头和结尾的所有单词?

c# - 为什么在通过 NuGet 添加时,某些程序集引用有版本而另一些则没有

c# - 检测在 C# 库中的主线程中运行

c# - SynchronizationContext 不再与 ExecutionContext 一起流动(从 .NET Framework 到 .NET Core)?