使用 StackTrace 获取调用方法
Get calling method using StackTrace
我有两个场景,我想知道哪个是我正在执行的某个方法的调用方方法。
这些是场景:
1)
public static void ExecuteMethod(object obj)
{
var mth = new StackTrace().GetFrame(1).GetMethod();
string methodName = mth.Name;
}
我这样称呼它:
public class Process
{
public int Handle { get; set; }
public string Name { get; set; }
public int ProcessID { get; set; }
public dynamic GetOwner()
{
return WMIMethod.ExecuteMethod(this);
}
}
当执行此操作时,methodName
的结果是我期望的结果:GetOwner
第二种有问题的情况是这种情况:
2)
public static dynamic ExecuteMethod(object obj, dynamic parameters)
{
var mth = new StackTrace().GetFrame(1).GetMethod();
string methodName = mth.Name;
}
我这样称呼它:
public class Process
{
public int Handle { get; set; }
public string Name { get; set; }
public int ProcessID { get; set; }
public dynamic GetOwner(dynamic inParams)
{
return WMIMethod.ExecuteMethod(this, inParams);
}
}
在这种情况下,当我调用 new Process().GetOwner(new { MyParam = "Something" } )
时,methodName
的结果不再是我期望的结果 (GetOwner
),取而代之的是 methodName
是 CallSite.Target
而 mth
的结果是
{Void CallSite.Target(System.Runtime.CompilerServices.Closure, System.Runtime.CompilerServices.CallSite, System.Type, ORMi.Sample.Models.Printer, System.Object)}
谁知道为什么第二种情况与第一种情况不同??。这个怎么解决???。
谢谢!
据我所知,当使用 dynamic
对象时,C# 添加了一个额外的方法调用 System.Dynamic.UpdateDelegates.UpdateAndExecute3()
.
在你的情况下,我将第二种方法重写为
public static dynamic ExecuteMethod(object obj, dynamic p)
{
var frame =
new StackTrace().GetFrames()
.Skip(1) // Skip the 'ExecuteMethod'
.First(x => x.GetMethod().DeclaringType.Namespace != "System.Dynamic");
return frame.GetMethod().Name;
}
不幸的是,我不知道为什么 C# 会插入该调用,所以如果有人能解释该内部工作,我将不胜感激。
我有两个场景,我想知道哪个是我正在执行的某个方法的调用方方法。
这些是场景:
1)
public static void ExecuteMethod(object obj)
{
var mth = new StackTrace().GetFrame(1).GetMethod();
string methodName = mth.Name;
}
我这样称呼它:
public class Process
{
public int Handle { get; set; }
public string Name { get; set; }
public int ProcessID { get; set; }
public dynamic GetOwner()
{
return WMIMethod.ExecuteMethod(this);
}
}
当执行此操作时,methodName
的结果是我期望的结果:GetOwner
第二种有问题的情况是这种情况:
2)
public static dynamic ExecuteMethod(object obj, dynamic parameters)
{
var mth = new StackTrace().GetFrame(1).GetMethod();
string methodName = mth.Name;
}
我这样称呼它:
public class Process
{
public int Handle { get; set; }
public string Name { get; set; }
public int ProcessID { get; set; }
public dynamic GetOwner(dynamic inParams)
{
return WMIMethod.ExecuteMethod(this, inParams);
}
}
在这种情况下,当我调用 new Process().GetOwner(new { MyParam = "Something" } )
时,methodName
的结果不再是我期望的结果 (GetOwner
),取而代之的是 methodName
是 CallSite.Target
而 mth
的结果是
{Void CallSite.Target(System.Runtime.CompilerServices.Closure, System.Runtime.CompilerServices.CallSite, System.Type, ORMi.Sample.Models.Printer, System.Object)}
谁知道为什么第二种情况与第一种情况不同??。这个怎么解决???。
谢谢!
据我所知,当使用 dynamic
对象时,C# 添加了一个额外的方法调用 System.Dynamic.UpdateDelegates.UpdateAndExecute3()
.
在你的情况下,我将第二种方法重写为
public static dynamic ExecuteMethod(object obj, dynamic p)
{
var frame =
new StackTrace().GetFrames()
.Skip(1) // Skip the 'ExecuteMethod'
.First(x => x.GetMethod().DeclaringType.Namespace != "System.Dynamic");
return frame.GetMethod().Name;
}
不幸的是,我不知道为什么 C# 会插入该调用,所以如果有人能解释该内部工作,我将不胜感激。