为什么System.DateTime结构具有布局类型Auto?

时间:2022-04-18 11:09:46

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

struct System.DateTime及其表兄System.DateTimeOffset的结构布局类型设置为“Auto”。这可以通过以下方式看出:

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#编写的struct(不是枚举的.NET值类型)将具有“Sequential”布局(除非已应用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有这个不寻常的结构布局?

1 个解决方案

#1


10  

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.

这需要推测,这个决定是在很久以前做出的,远在.NET 1.0发布之前。 System.DateTime上的属性充其量只是一种微优化,在.NET代码中并不罕见。这有点合适,结构只有一个字段,所以布局从来没有任何问题。内部CustomAttribute结构的结构可能是由同一个程序员完成的。无关紧要,非托管代码永远不会看到它们。

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. 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.

那个程序员逃脱了它,没有理由让CLR从顺序版本重新安排布局。当struct在字段之间包含足够大以适合另一个小字段的填充时,会重新安排自动布局。不是DateTimeOffet的情况。

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

当你提交DateTimeOffset的反馈报告时,你会得到微软大师注意这一点的一些可能性。这是错误的afaik。将其发布到connect.microsoft.com

#1


10  

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.

这需要推测,这个决定是在很久以前做出的,远在.NET 1.0发布之前。 System.DateTime上的属性充其量只是一种微优化,在.NET代码中并不罕见。这有点合适,结构只有一个字段,所以布局从来没有任何问题。内部CustomAttribute结构的结构可能是由同一个程序员完成的。无关紧要,非托管代码永远不会看到它们。

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. 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.

那个程序员逃脱了它,没有理由让CLR从顺序版本重新安排布局。当struct在字段之间包含足够大以适合另一个小字段的填充时,会重新安排自动布局。不是DateTimeOffet的情况。

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

当你提交DateTimeOffset的反馈报告时,你会得到微软大师注意这一点的一些可能性。这是错误的afaik。将其发布到connect.microsoft.com