I am trying to find a solution to ‘break into non-public methods’.
I just want to call RuntimeMethodInfo.InternalGetCurrentMethod(...), passing my own parameter (so I can implement GetCallingMethod()), or directly use RuntimeMethodInfo.InternatGetCurrentMethod(ref StackCrawlMark.LookForMyCaller) in my logging routines. GetCurrentMethod is implemented as:
[MethodImpl(MethodImplOptions.NoInlining)]
public static MethodBase GetCurrentMethod()
{
StackCrawlMark lookForMyCaller = StackCrawlMark.LookForMyCaller;
return RuntimeMethodInfo.InternalGetCurrentMethod(ref lookForMyCaller);
}
where InternalGetCurrentMethod is declared: internal :-).
I have no problem calling the method using reflection, but this messes up the call stack and that is just the one thing that has to be preserved, otherwise it defeats its purpose.
What are my odds of keeping the stacktrace close to the original (at least within the distance of the allowed StackCrawlMarks, which are LookForMe, LookForMyCaller and LookForMyCallersCaller. Is there some complex way to achieve what I want?
If there’s one thing I love about C#, it’s dynamic methods.
They let you bypass every goal and intention of the .NET creators. 😀
Here’s a (thread-safe) solution:
(Eric Lippert, please don’t read this…)