静态构造函数是否保证不会被内联?
大概吧!
编辑:这个问题的动机是:在我看来,由于静态构造函数的语义,它们永远无法安全地内联。
I guess so!
EDIT: The motivation for this question is: It seems to me that due to the semantics of static constructors they could never be safely inlined.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
抖动的内联策略是一个实现细节 - 随时可能更改 - 因此几乎无法保证什么可以或不可以内联。
话虽如此,考虑到 C# 和 CLI 规范提供的有关静态构造函数和类型初始化的保证,很难看出如何安全地内联静态构造函数。
来自 Microsoft C# 规范(第 10.12 节):
并来自 ECMA CLI 规范(第 8.9.5 节):
(请注意,具有静态构造函数的 C# 类不会具有
beforefieldinit
语义。没有静态构造函数的 C# 类将具有beforefieldinit
语义。)The jitter's inlining strategy is an implementation detail -- subject to change at any time -- so there are pretty much no guarantees about what can or can't be inlined.
Having said that, it's difficult to see how a static constructor could ever be safely inlined, bearing in mind the guarantees provided by the C# and CLI specs regarding static constructors and type initialisation.
From the Microsoft C# specification (section 10.12):
And from the ECMA CLI specification (section 8.9.5):
(Note that C# classes with a static constructor will not have
beforefieldinit
semantics. C# classes without a static constructor will havebeforefieldinit
semantics.)您拥有的唯一保证如下:
The only guarantees you have are the following :