[Mono-dev] Seeking advice on converting basic value types from the CLR representation to native C types, using mono embedding

Robert Jordan robertj at gmx.net
Thu Oct 11 10:55:46 UTC 2012


Hi,

On 11.10.2012 04:45, Jean-Michel.Perraud at csiro.au wrote:
>
> I am working on a package to access the Mono (resp. MS.NET) CLR from
> the R statistical language. I am trying to convert 'simple' CLR value
> types (e.g. string, double, bool, DateTime) to marshall them to their
> R equivalent. I managed to deal with the 'bool', but stumbled on the
> DateTime


DateTime is a "real" value type (in contrast to "double", "bool" etc.
which are primitive types). This means that whenever you unbox
an object of this type you'd need its unmanged representation,
something like that:

typedef struct _MonoDateTime {
	gint64 encoded;
} MonoDateTime;


Obviously this is pretty "opaque" as you've probably no
idea how the field "encoded" is actually encoded ;) Also, it's
unsafe to rely on undocumented layouts, because they may
change w/out notice (although I believe DateTime won't
change any time soon).

What you can safely do is: implement a helper method
in C# which returns an R date-time from a .NET DateTime.
It's probably just a matter of converting DateTime.Ticks from
the .NET epoch to R's epoch.

Of course, you could also invoke the Ticks getter from C,
but it's usually easier and faster to provide helper methods.

Robert



More information about the Mono-devel-list mailing list