【发布时间】:2011-07-05 19:27:35
【问题描述】:
我正在努力寻找“闯入非公共方法”的解决方案。
我只想调用RuntimeMethodInfo.InternalGetCurrentMethod(...),传递我自己的参数(这样我就可以实现GetCallingMethod()),或者在我的日志记录例程中直接使用RuntimeMethodInfo.InternatGetCurrentMethod(ref StackCrawlMark.LookForMyCaller)。 GetCurrentMethod 实现为:
[MethodImpl(MethodImplOptions.NoInlining)]
public static MethodBase GetCurrentMethod()
{
StackCrawlMark lookForMyCaller = StackCrawlMark.LookForMyCaller;
return RuntimeMethodInfo.InternalGetCurrentMethod(ref lookForMyCaller);
}
InternalGetCurrentMethod 的声明位置:内部 :-)。
我使用反射调用该方法没有问题,但这会弄乱调用堆栈,而这只是必须保留的一件事,否则它会失去其目的。
保持堆栈跟踪接近原始的可能性有多大(至少在允许的StackCrawlMarks 的距离内,即LookForMe、LookForMyCaller 和LookForMyCallersCaller。有没有一些复杂的方法来实现我想要什么?
【问题讨论】:
标签: c# reflection methods private