我有两个场景,我想知道哪个是我正在执行的某个方法的调用者方法。
以下是一些场景:
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)}
谁知道为什么第二种情况不同于第一种情况?这个问题怎么解决?
谢谢!
https://stackoverflow.com/questions/51011107
复制相似问题