为什么DateTime无法播放? [英] Why is DateTime not blittable?

查看:62
本文介绍了为什么DateTime无法播放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对某些事情不清楚.

Decimal和DateTime只是结构,Decimal类型包含四个Int32字段,并且因为它们是可blittable的,所以Decimal确实是可blittable的.

Decimal and DateTime are just structs, the Decimal type contains four Int32 fields, and because these are blittable a Decimal is indeed blittable.

DateTime也是一个结构,它包含一个UInt64字段,因为它是可被Blittable的,所以我希望DateTime是可被Blittable的,但是它不是.

DateTime is a struct too, it contains a single UInt64 field, because this is blittable I'd expect DateTime to be blittable but it isn't.

我的理解是,任何具有LayoutKind.Sequential并且仅包含blittable字段的结构或类本身都是blittable.

My understanding is that any struct or class that has LayoutKind.Sequential AND contains ONLY blittable fields, is itself blittable.

我对此的研究表明,尽管DateTime是一个结构,但并未使用LayoutKind.Sequential进行格式化,这就是为什么它不能被blittable的原因.似乎已经用自动"布局定义了,这就是它无法变色的原因.

My research into this suggests that DateTime, although it is a struct, is not formatted with LayoutKind.Sequential and this is why it isn't blittable. It seems to have been defined with Auto layout and this is the reason it isn't blittable.

但是,我不知道MS如何或为什么这样做,C#语言规则说所有结构都具有LayoutKind.Sequential,是默认使用的.

However, I don't know how or why this was done by MS, the C# language rules say that ALL structs have LayoutKind.Sequential applied BY DEFAULT.

有人从DateTime那里了解这种奇怪的行为吗?

Does anyone know more about this odd behaviour from DateTime?

顺便说一句,您可以通过调用以下命令轻松测试blittable:

Incidentally, you can test for blittable easily by just calling:

GCHandle pinStructure = GCHandle.Alloc(item, GCHandleType.Pinned);

如果未引发异常,则"item"的类型为可蓝变的类型.

If no exception is thrown, then the type of 'item' is a blittable type.

我怀疑他们故意将DateTime设置为不可复制,以避免与现有Win32日期时间结构混淆,使DateTime可以被设置为可复制状态使人们相信他们可以将其编组为这些Win32类型之一.

I suspect that they deliberately made DateTime non-blittable to avoid confusion with existing Win32 date time structs, making DateTime blittable migh lead people to believe they can marshal it to one of these Win32 types.

Cap'n

 

 

 

推荐答案

  谢谢你的帖子.我使用.NET反射器检查DateTime和Decimal结构的定义,如下所示:

   Thanks for your post. I use the .NET reflector to check the definitions of DateTime and Decimal struct as below:

   DateTime :

   DateTime:

[Serializable]
public struct DateTime : IComparable, IFormattable, IConvertible, ISerializable, IComparable<DateTime>, IEquatable<DateTime>
{
 // Fields
 private ulong dateData;
 private const string DateDataField = "dateData";
 private const int DatePartDay = 3;
 private const int DatePartDayOfYear = 1;
 private const int DatePartMonth = 2;
 private const int DatePartYear = 0;
 private const int DaysPer100Years = 0x8eac;
 private const int DaysPer400Years = 0x23ab1;
 private const int DaysPer4Years = 0x5b5;
 private const int DaysPerYear = 0x16d;
 private const int DaysTo10000 = 0x37b9db;
 private const int DaysTo1601 = 0x8eac4;
 private const int DaysTo1899 = 0xa9559;
 private static readonly int[] DaysToMonth365;
 private static readonly int[] DaysToMonth366;
 private const long DoubleDateOffset = 0x85103c0cb83c000L;
 private const long FileTimeOffset = 0x701ce1722770000L;
 private const ulong FlagsMask = 13835058055282163712L;
 private const ulong KindLocal = 9223372036854775808L;
 private const ulong KindLocalAmbiguousDst = 13835058055282163712L;
 private const int KindShift = 0x3e;
 private const ulong KindUnspecified = 0L;
 private const ulong KindUtc = 0x4000000000000000L;
 private const ulong LocalMask = 9223372036854775808L;
 private const long MaxMillis = 0x11efae44cb400L;
 internal const long MaxTicks = 0x2bca2875f4373fffL;
 public static readonly DateTime MaxValue;
 private const int MillisPerDay = 0x5265c00;
 private const int MillisPerHour = 0x36ee80;
 private const int MillisPerMinute = 0xea60;
 private const int MillisPerSecond = 0x3e8;
 internal const long MinTicks = 0L;
 public static readonly DateTime MinValue;
 private const double OADateMaxAsDouble = 2958466.0;
 private const double OADateMinAsDouble = -657435.0;
 private const long OADateMinAsTicks = 0x6efdddaec64000L;
 private const long TicksCeiling = 0x4000000000000000L;
 private const string TicksField = "ticks";
 private const ulong TicksMask = 0x3fffffffffffffffL;
 private const long TicksPerDay = 0xc92a69c000L;
 private const long TicksPerHour = 0x861c46800L;
 private const long TicksPerMillisecond = 0x2710L;
 private const long TicksPerMinute = 0x23c34600L;
 private const long TicksPerSecond = 0x989680L;

 // Methods
 .....}

 

   十进制:

   Decimal:

[Serializable, StructLayout(LayoutKind.Sequential), ComVisible(true)]
public struct Decimal : IFormattable, IComparable, IConvertible, IComparable<decimal>, IEquatable<decimal>
{
 private const int SignMask = -2147483648;
 private const int ScaleMask = 0xff0000;
 private const int ScaleShift = 0x10;
 private const int MaxInt32Scale = 9;
 [DecimalConstant(0, 0, (uint) 0, (uint) 0, (uint) 0)]
 public static readonly decimal Zero;
 [DecimalConstant(0, 0, (uint) 0, (uint) 0, (uint) 1)]
 public static readonly decimal One;
 [DecimalConstant(0, 0x80, (uint) 0, (uint) 0, (uint) 1)]
 public static readonly decimal MinusOne;
 [DecimalConstant(0, 0, uint.MaxValue, uint.MaxValue, uint.MaxValue)]
 public static readonly decimal MaxValue;
 [DecimalConstant(0, 0x80, uint.MaxValue, uint.MaxValue, uint.MaxValue)]
 public static readonly decimal MinValue;
 private static uint[] Powers10;
 private int flags;
 private int hi;
 private int lo;
 private int mid;
 //Methods
 ...
}

   如我们所见,十进制类型不仅包含四个.Int反射器中显示的Int32字段.而且DateTime也不仅包含单个UInt64字段.但是,十进制类型中的所有字段都是可变数的, 而DateTime则不是.

   As we can see, Decimal type doesn't only contain four Int32 fields as it shown in the .NET reflector. And DateTime doesn't only contain a single UInt64 field either. But all the fields in the Decimal type are blittable, while DateTime's are not.

     DateTime包含几个String类型字段.根据 在这里,字符串不可变.因此,我认为这是DateTime无法播放的原因.希望这可以帮助您解决这个问题.

    DateTime contains several String type fields. According to here, String is non-blittable. So I think this is the reason DateTime is not blittable. Hope this could help you through this.

 

 


这篇关于为什么DateTime无法播放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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