简体   繁体   English

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

[英]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程序集,而DateTimeDateTimeOffset是我在这个布局中找到的唯一公开可见的结构。

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.

相关问题 System.DateTime种类比特 - System.DateTime Kind Bits 使用DateTime为什么有时需要将System.DateTime放在Visual Studio 2012中? - Using DateTime why do I sometimes have to put System.DateTime in Visual Studio 2012? “ System.DateTime”不包含“文本”的定义 - 'System.DateTime' does not contain a definition for 'Text' System.DateTime? vs System.DateTime - System.DateTime? vs System.DateTime 为什么我的列表显示每个对象都是“System.Collections.Generic.List`1[System.DateTime]” - Why does my list display with each object being "System.Collections.Generic.List`1[System.DateTime]" 阻止Xamarin自动将System.DateTime转换为NSDate? - Prevent Xamarin from auto converting System.DateTime to NSDate? 对象结果<Nullable<System.DateTime> &gt; 没有得到存储过程结果 - ObjectResult<Nullable<System.DateTime>> does not getting stored procedure result 列表模型 - &gt; orderbydescending System.DateTime不起作用? - List model --> orderbydescending System.DateTime does not work? 为什么C#中没有System.DateTime的“日期”简写? - Why is there no "date" shorthand of System.DateTime in C#? LINQ to Entities 无法识别方法“System.DateTime GetValueOrDefault()” - LINQ to Entities does not recognize the method 'System.DateTime GetValueOrDefault()'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM