[英]Why does the System.DateTime struct have layout kind Auto?
The struct System.DateTime
and its cousin System.DateTimeOffset
have their structure layout kinds set to "Auto". struct
System.DateTime
及其表兄System.DateTimeOffset
的结构布局类型设置为“Auto”。 This can be seen with: 这可以看作:
typeof(DateTime).IsAutoLayout /* true */
or: 要么:
typeof(DateTime).StructLayoutAttribute.Value /* Auto */
or it can be seen from the IL which declares: 或者从IL中可以看出它声明:
.class public auto ansi serializable sealed beforefieldinit System.DateTime
¯¯¯¯
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). 通常,使用C#编写的结构(即不是枚举的.NET值类型)将具有“顺序”布局(除非已应用
StructLayoutAttribute
来指定另一个布局)。
I searched through some common BCL assemblies, and DateTime
and DateTimeOffset
were the only publicly visible structs I found with this layout. 我搜索了一些常见的BCL程序集,而
DateTime
和DateTimeOffset
是我在这个布局中找到的唯一公开可见的结构。
Does anyone know why DateTime
has this unusual struct layout? 有谁知道为什么
DateTime
有这种不寻常的结构布局?
This is going to require speculation, this decision was made a long time ago, well before .NET 1.0 shipped. 这需要推测,这个决定是在很久以前做出的,远在.NET 1.0发布之前。 The attribute on System.DateTime is at best a micro-optimization, not uncommon in .NET code.
System.DateTime上的属性充其量只是一种微优化,在.NET代码中并不罕见。 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.
内部CustomAttribute结构的结构可能是由同一个程序员完成的。 Doesn't matter either, unmanaged code never sees them.
无关紧要,非托管代码永远不会看到它们。
The one for System.DateTimeOffset was done much later and almost certainly a copy-paste bug. System.DateTimeOffset的那个以后完成了,几乎可以肯定是一个复制粘贴错误。
That programmer got away with it, no reason for the CLR to re-arrange the layout from the sequential version. 那个程序员逃脱了它,没有理由让CLR从顺序版本重新安排布局。 Re-arranging with auto-layout occurs when the struct contains padding between fields that is large enough to fit another small field.
当struct在字段之间包含足够大以适合另一个小字段的填充时,会重新安排自动布局。 Not the case for DateTimeOffet.
不是DateTimeOffet的情况。
Some odds you'll get a Microsoft guru to pay attention to this when you file a feedback report for DateTimeOffset. 当你提交DateTimeOffset的反馈报告时,你会得到微软大师注意的一些可能性。 It is wrong afaik.
这是错误的afaik。 Post it to connect.microsoft.com
将其发布到connect.microsoft.com
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.