C# Marshal.StructureToPtr<-&燃气轮机;PTRTO日期时间字段
结构C# Marshal.StructureToPtr<-&燃气轮机;PTRTO日期时间字段,c#,.net,marshalling,C#,.net,Marshalling,结构 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;
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;
}
}
public结构勾选:IEquatable
{
公共日期时间日期;
公共十进制价格;
公共int卷;
公共刻度(日期时间日期、十进制价格、整数体积)
{
this.date=日期;
这个价格=价格;
这个。体积=体积;
}
公共覆盖布尔等于(对象对象对象)
{
其他变量=(勾选)obj;
返回this.date==other.date&&this.price==other.price&&this.volume==other.volume;
}
公共布尔等于(勾选其他)
{
返回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
[测试]
public void marshalldoesntroundsdatetime(){
对于(int i=0;i<1000;i++)
{
var now=新勾号(DateTime.now.AddSeconds(i),i,i);
var now2=现在;
var ticks=新的Tick[1];
不安全的
{
已修复(勾号*ptr=&勾号[0])
{
Marshal.StructureToPtr(now2,(IntPtr)ptr,false);
now2=(Tick)Marshal.ptr结构((IntPtr)ptr,typeof(Tick));
Assert.AreEqual(now.date.Ticks,now2.date.Ticks);
}
}
}
}
预计:635719676058860752
但是是:635719676058860000
发生了什么事?为什么日期时间在编组后四舍五入?这在什么地方有记录吗?真正的错误是
DateTime
不应该被封送。。。如果您尝试直接对其进行封送处理
,则会得到一个ArgumentException
如果您真的想封送
日期时间(考虑到它是.NET的半专有格式,我甚至不想知道为什么),您可以:
public long date;
public DateTime Date
{
get
{
return DateTime.FromBinary(date);
}
set
{
date = value.ToBinary();
}
}
真正的错误是
DateTime
不应被封送。。。如果您尝试直接对其进行封送处理
,则会得到一个ArgumentException
如果您真的想封送
日期时间(考虑到它是.NET的半专有格式,我甚至不想知道为什么),您可以:
public long date;
public DateTime Date
{
get
{
return DateTime.FromBinary(date);
}
set
{
date = value.ToBinary();
}
}
真正的错误是
DateTime
不应被封送。。。如果您尝试直接对其进行封送处理
,则会得到一个ArgumentException
如果您真的想封送
日期时间(考虑到它是.NET的半专有格式,我甚至不想知道为什么),您可以:
public long date;
public DateTime Date
{
get
{
return DateTime.FromBinary(date);
}
set
{
date = value.ToBinary();
}
}
真正的错误是
DateTime
不应被封送。。。如果您尝试直接对其进行封送处理
,则会得到一个ArgumentException
如果您真的想封送
日期时间(考虑到它是.NET的半专有格式,我甚至不想知道为什么),您可以:
public long date;
public DateTime Date
{
get
{
return DateTime.FromBinary(date);
}
set
{
date = value.ToBinary();
}
}
StructureToPtr()用于封送非托管代码的数据。本机代码中的日期有多个“标准”,没有一个在范围和精度上接近DateTime。CLR设计者选择了COM互操作标准,该标准也是由DateTime.ToOADate()公开的
从中可以看出,它的精度不超过1毫秒。DateTime精确到0.1 usec。不可避免地,您正在查看的最后4位数字必须为0
现在还不清楚你为什么要这样做,或者为什么这很重要。猜测一下,请记住Marshal.StructureToPtr()似乎只是序列化.NET数据的一种很有吸引力的方式。Marshal.StructureToPtr()旨在为非托管代码封送数据。本机代码中的日期有多个“标准”,没有一个在范围和精度上接近DateTime。CLR设计者选择了COM互操作标准,该标准也是由DateTime.ToOADate()公开的
从中可以看出,它的精度不超过1毫秒。DateTime精确到0.1 usec。不可避免地,您正在查看的最后4位数字必须为0
现在还不清楚你为什么要这样做,或者为什么这很重要。猜测一下,请记住Marshal.StructureToPtr()似乎只是序列化.NET数据的一种很有吸引力的方式。Marshal.StructureToPtr()旨在为非托管代码封送数据。本机代码中的日期有多个“标准”,没有一个在范围和精度上接近DateTime。CLR设计者选择了COM互操作标准,该标准也是由DateTime.ToOADate()公开的
从中可以看出,它的精度不超过1毫秒。DateTime精确到0.1 usec。不可避免地,您正在查看的最后4位数字必须为0
现在还不清楚你为什么要这样做,或者为什么这很重要。猜测一下,请记住Marshal.StructureToPtr()似乎只是序列化.NET数据的一种很有吸引力的方式。Marshal.StructureToPtr()旨在为非托管代码封送数据。本机代码中的日期有多个“标准”,没有一个在范围和精度上接近DateTime。CLR设计者选择了COM互操作标准,该标准也是由DateTime.ToOADate()公开的
从中可以看出,它的精度不超过1毫秒。DateTime精确到0.1 usec。不可避免地,您正在查看的最后4位数字必须为0
现在还不清楚你为什么要这样做,或者为什么这很重要。猜猜看,请记住Marshal.StructureToPtr()似乎只是序列化.NET数据的一种很有吸引力的方式。为什么要覆盖该结构的等式?结构的默认行为是比较它们的字段。@Damien_不相信这是一个理智的检查,因为我无法理解为什么它们不相等,直到我查看了tick。你到底为什么要覆盖该结构的相等?结构的默认行为是比较它们的字段。@Damien_不相信这是一个理智的检查,因为我无法理解为什么它们不相等,直到我研究了滴答声。你到底为什么要覆盖这些字段的相等