[英]Marshal.StructureToPtr <-> PtrToStructure rounds DateTime field
結構
public struct Tick : IEquatable<Tick>
{
public DateTime date;
public decimal price;
public int volume;
public Tick(DateTime date, decimal price, int volume)
{
this.date = date;
this.price = price;
this.volume = volume;
}
public override bool Equals(object obj)
{
var other = (Tick)obj;
return this.date == other.date && this.price == other.price && this.volume == other.volume;
}
public bool Equals(Tick other)
{
return this.date == other.date && this.price == other.price && this.volume == other.volume;
}
}
在此測試中進行了更改:
[Test]
public void MarshalDoesntRoundsDateTime() {
for (int i = 0; i < 1000; i++)
{
var now = new Tick(DateTime.Now.AddSeconds(i), i, i);
var now2 = now;
var ticks = new Tick[1];
unsafe
{
fixed (Tick* ptr = &ticks[0])
{
Marshal.StructureToPtr(now2, (IntPtr)ptr, false);
now2 = (Tick)Marshal.PtrToStructure((IntPtr)ptr, typeof(Tick));
Assert.AreEqual(now.date.Ticks, now2.date.Ticks);
}
}
}
}
Expected: 635719676058860752
But was: 635719676058860000
到底是怎么回事? 為什么日期時間在編組后四舍五入? 這是在某處記錄的嗎?
Marshal.StructureToPtr()用於封送非托管代碼的數據。 對於本機代碼中的日期,有多個“標准”,沒有一個在范圍和准確性上與DateTime接近。 CLR設計人員采用了COM互操作標准,該標准也由DateTime.ToOADate()公開。
從參考源可以看出,它的准確度不能超過1毫秒。 日期時間精確到0.1微秒。 不可避免地,您要查看的最后4位數字必須為0。
目前尚不清楚您為什么這樣做或為何如此重要。 猜測,請記住,Marshal.StructureToPtr() 似乎只是一種有吸引力的序列化.NET數據的方式。
真正的錯誤是, DateTime
不宜marshalable ......如果你試圖Marshal
它直接你會得到一個ArgumentException
。
如果您真的想Marshal
一個DateTime
(並且我什至不想知道為什么,考慮到它是.NET的半專有格式),則可以:
public long date;
public DateTime Date
{
get
{
return DateTime.FromBinary(date);
}
set
{
date = value.ToBinary();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.