在方法中使用 System.Reflection.MethodBase.GetCurrentMethod() 是否会阻止 JIT 编译器内联该方法? [英] Does using System.Reflection.MethodBase.GetCurrentMethod() in a method prevent the JIT compiler from inlining the method?

查看:53
本文介绍了在方法中使用 System.Reflection.MethodBase.GetCurrentMethod() 是否会阻止 JIT 编译器内联该方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下问题中的答案和评论提供了相互矛盾的信息.

The answers and comments in the following questions provide conflicting information.

如何获取来自代码的当前方法

你能用反射查找当前正在执行的方法的名称?

文档中也没有提及.

https://msdn.microsoft.com/en-us/library/system.reflection.methodbase.getcurrentmethod.aspx

推荐答案

不,JIT 编译器可以自由地内联方法.

No, the JIT compiler is free to inline the method.

您需要将 [MethodImpl(MethodImplOptions.NoInlining)] 添加到任何调用 GetCurrentMethod() 的方法中,以防止其被内联.

You'll need to add the [MethodImpl(MethodImplOptions.NoInlining)] to any method that calls GetCurrentMethod() to prevent it from being inlined.

如果您只对方法名称感兴趣,CallerMemberNameAttribute 更容易使用.

If you're only interested in the method name, CallerMemberNameAttribute is much easier to use.

这篇关于在方法中使用 System.Reflection.MethodBase.GetCurrentMethod() 是否会阻止 JIT 编译器内联该方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆