当被MethodBase.GetCurrentMethod可靠/ predictable?可靠、MethodBase、GetCurrentMethod、predictable

由网友(暮念)分享简介:有一个方法可以得到内联;有一个属性,以prevent说(有一个ATT为)。然而,很明显的方法可能也无法获得在x64自己的堆栈帧归因于抖动尾部调用优化(http://www.hanselman.com/blog/ReleaseISNOTDebug64bitOptimizationsAndCMethodInliningIn...

有一个方法可以得到内联;有一个属性,以prevent说(有一个ATT为)。然而,很明显的方法可能也无法获得在x64自己的堆栈帧归因于抖动尾部调用优化(http://www.hanselman.com/blog/ReleaseISNOTDebug64bitOptimizationsAndCMethodInliningInReleaseBuildCallStacks.aspx).这会影响到 MethodBase.GetCurrentMethod

A method could get inlined; there is an attribute to prevent that ("there's an att for that"). However, apparently a method may also not get its own stack frame on x64 due to tail-call optimization by the JITter (http://www.hanselman.com/blog/ReleaseISNOTDebug64bitOptimizationsAndCMethodInliningInReleaseBuildCallStacks.aspx). Would this affect the behavior of MethodBase.GetCurrentMethod?

这是我能找到的讨论大多关于内联(When是一种方法资格由CLR被内联?)。虽然这些讨论都是在自己的权利有趣的,我的问题实际上是在什么情况下 - 如果有的话 - 那 MethodBase.GetCurrentMethod 可以依靠,以确定同样的方法其中编程打的电​​话(例如,对于后期绑定到一个方法,使目前的方法是一个真正的替代品)。内联是一种方式, MethodBase.GetCurrentMethod 可能被愚弄,但我不知道这是唯一的出路?

The discussions that I can find are mostly about inlining (When is a method eligible to be inlined by the CLR?). While those discussions are interesting in their own right, my problem is really about under what circumstances -- if any -- that MethodBase.GetCurrentMethod can be relied upon to identify the same method where the programmer placed the call (e.g., for late binding to a method for which the current method is really a surrogate). Inlining is a way that MethodBase.GetCurrentMethod could be fooled, but I wonder if it is the only way?

推荐答案

没有 - 的方法要么是内联在运行时或者它不是

No - a method is either inlined at run time or it isn't.

它可以被愚弄,我相信,如果有人要实施自己的时间 - 因为 MethodBase.GetCurrentMethod 最终归结到这一点,在 RuntimeMethodHandle :

It can be fooled, I believe, if somebody were to implement their own runtime - as MethodBase.GetCurrentMethod ultimately boils down to this, declared in RuntimeMethodHandle:

[SecurityCritical]
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern IRuntimeMethodInfo 
  _GetCurrentMethod(ref StackCrawlMark stackMark);

在自定义运行时,可能会做任何事情 - 和将来它也可能做任何事情了。此刻,我相信微软运行时(S)将不会返回正确的方法,唯一的办法是,如果code为内联;不能为单声道说话,例如。依靠这始终是这种情况,但是,就像是依靠内部类型的反射私人领域始终是present,使一块code的工作,我想。

In a custom runtime, that could do anything - and in the future it could also do anything too. At the moment, I believe the only way that the Microsoft runtime(s) will not return the correct method is if the code is inlined; can't speak for Mono, for example. Relying on that always to be the case, however, is like relying on a reflected private field of an internal type always to be present to enable a piece of code to work, I think.

在几乎所有情况下,我都试过(我不再担心现在),或有人试图证明需要才能够可靠地识别调用方法,分析外,中联方法的问题总是出现。

In nearly every case where I have tried (I no longer worry about it now) or someone tries to justify needing to be able to reliably identify the calling method, outside of profiling, the issue of inlined methods always comes up.

现实情况是,虽然多么重要的是它担心这些方法?

The reality is, though, how important is it to worry about those methods?

如果可靠性是绝对重要的,你会得到更多的运气使用IL-重写后编译注入的通话记录;这可以是一定的方法感知,因此可以颠覆内联,即使发生(其可能不如果重写的IL变得足够体积大,从而损害性能)。或者,如果你不喜欢滚动您自己,一个AOP库可能是为了 - 比如 PostSharp 。

If reliability is absolutely crucial, you will get more luck Using an IL-rewriter post compilation to inject the logging calls; this can be necessarily method-aware and therefore can subvert the inlining, even if it takes place (which it might not if the rewritten IL becomes bulky enough, thus hurting performance). Or if you don't fancy rolling your own, an AOP library might be in order - such as PostSharp.

阅读全文

相关推荐

最新文章