c# - Marshal.StructureToPtr <-> PtrToStructure rounds DateTime field -


the structure

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;     } } 

is changed in test:

    [test]     public void marshaldoesntroundsdatetime() {         (int = 0; < 1000; i++)         {             var = 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   was:  635719676058860000 

what going on? why datetime rounded after marshalling? documented somewhere?

marshal.structuretoptr() intended marshal data unmanaged code. there multiple "standards" dates in native code, none close in range , accuracy datetime. clr designers went com interop standard, exposed datetime.tooadate().

as can tell reference source, can no more accurate 1 msec. datetime accurate 0.1 usec. inevitably last 4 digits looking @ must 0.

it not clear why doing or why matters. guessing, keep in mind marshal.structuretoptr() seems attractive way serialize .net data.


Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

android - Pass an Serializable object in AIDL -