为何System.DateTime的结构具有布局样的汽车? [英] Why does the System.DateTime struct have layout kind Auto?

查看:213
本文介绍了为何System.DateTime的结构具有布局样的汽车?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该结构 的System.DateTime 及其表弟 System.DateTimeOffset 有它们的结构布局种设置为自动。这可以看出,与

The struct System.DateTime and its cousin System.DateTimeOffset have their structure layout kinds set to "Auto". This can be seen with:

typeof(DateTime).IsAutoLayout    /* true */

typeof(DateTime).StructLayoutAttribute.Value    /* Auto */

,或者它可以从以下的IL它声明可以看出:

or it can be seen from the IL which declares:

.class public auto ansi serializable sealed beforefieldinit System.DateTime
              ¯¯¯¯

通常,结构(即这是不是一个枚举一个.NET值类型)用C#将布局顺序(除非<一个href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.structlayoutattribute.aspx"><$c$c>StructLayoutAttribute已经被应用到指定另一个布局)。

Normally a struct (that is a .NET value type which is not an enum) written with C# will have layout "Sequential" (unless a StructLayoutAttribute has been applied to specify another layout).

我通过一些常见的 BCL 组件和的DateTime 的DateTimeOffset 是唯一公开的结构,我发现用这个布局。

I searched through some common BCL assemblies, and DateTime and DateTimeOffset were the only publicly visible structs I found with this layout.

有谁知道为什么的DateTime 有这个不同寻常的结构布局?

Does anyone know why DateTime has this unusual struct layout?

推荐答案

这将需要炒作,作出这个决定一的的时间前,以及之前的.NET 1.0发布。在System.DateTime的属性是最好的一个微型的优化,在.NET code并不少见。这是有点为宜,结构只有一个字段,以便有从未与布局的任何问题。那些为内部CustomAttribute结构由相同的程序员很可能进行。无所谓要么,非托管code永远看不到它们。

This is going to require speculation, this decision was made a long time ago, well before .NET 1.0 shipped. The attribute on System.DateTime is at best a micro-optimization, not uncommon in .NET code. It is somewhat appropriate, the struct has only one field so there's never any issue with layout. The ones for the internal CustomAttribute structs were probably done by the same programmer. Doesn't matter either, unmanaged code never sees them.

一个为System.DateTimeOffset做很久以后,几乎肯定是复制粘贴的bug。

The one for System.DateTimeOffset was done much later and almost certainly a copy-paste bug.

这是程序员带走了它,没有理由CLR重新安排的顺序版本的布局。重新安排与自动布局时,就会发生结构包含字段之间填充足够大,以适应另一个小场。不是这样的DateTimeOffet。

That programmer got away with it, no reason for the CLR to re-arrange the layout from the sequential version. Re-arranging with auto-layout occurs when the struct contains padding between fields that is large enough to fit another small field. Not the case for DateTimeOffet.

有些胜算,你会得到一个微软大师要注意这个,当你提交反馈报告的DateTimeOffset。这是错误的AFAIK。发布到connect.microsoft.com

Some odds you'll get a Microsoft guru to pay attention to this when you file a feedback report for DateTimeOffset. It is wrong afaik. Post it to connect.microsoft.com

这篇关于为何System.DateTime的结构具有布局样的汽车?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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