[Mono-dev] Marshaling DateTime type

Raja R Harinath harinath at hurrynot.org
Wed Oct 15 11:41:09 EDT 2008


Hi,

Robert Jordan <robertj at gmx.net> writes:
> Raja R Harinath wrote:
>> Robert Jordan <robertj at gmx.net> writes:
>>> FirstName LastName wrote:
>>>> What would be the best equivalent unmanaged type (linux and windows)
>>>> to use if I want to marshal a DateTime in the managed world (.NET,
>>>> mono)?
>>> You can't marshal DateTime directly because its internal representation
>>> is different between runtimes *and* profiles. Use DateTime.Tick (an
>>> int64) or a similar era-based representation.
>> 
>> But, IIRC, strangely enough, arrays of DateTimes should serialize fine.
>> So, you might be better of using a one-element DateTime array.
>
> Look at DateTime's layout:
>
> public struct DateTime
> {
>          private TimeSpan ticks;
> #if NET_2_0
>         	DateTimeKind kind;
> #endif
> }
>
> Nasty things would happen if the unmanaged code is profile agnostic.

>From a quick examination of the source code in SVN trunk, my
understanding is that both the Binary and Soap serializers have special
cases for DateTime, which aren't dependent on the exact layout -- and
the serialization is profile-dependent.

(Of course, since I haven't actually tried it out, I can't guarantee
that it's fully interoperable with .NET.)

- Hari



More information about the Mono-devel-list mailing list