I know one can use an index into an array to specify length of the array for a marshaled C-array. However, I would like to do it a little different.
I would like the size to be a prefixed Int16. If I make it an entry of the array, I can’t control the marshaled size of the count specifier.
So, in short, how do I write a custom marshaller that prefixes an Int16 as a count.
Please note, I have to serialize the data, so no IntPtrs are allowed.
The part I’m stumped on, is how to implement GetNativeDataSize. I don’t have an IntPtr or Managed Object at that point, so how will I be able to marshal out the Int16 to get the count.
For example.
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
class Something
{
[MarshalAs(UnmanagedType.CustomMarshaler ... ]
public ArrayItem[];
}
And the other class
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
class ArrayItem
{
public int Item;
}
In Native side, this would appear as
struct
{
short count;
int[] Item;
}
But since every variable length array on managed side would do this, I would like a custom marshaller to append the count for me.
Problems are
- I don’t know what GetNativeDataSize should do, or how it will work, since I don’t have any reference to the native data.
- I can’t rely on the LPArray, because the count must be an Int16
- Will the array items marshal correctly, or does the array custom marshaller hide the items marshaller, or am I going to have to implement some generic marshaller (if that’s possible).
I’m gonna say it…a custom marshaler feels like overkill. If you really want to do this with a custom marshaler, you’re going to have to use
unsafeblocks/methods and pointer math to write the data.Why not have a class similar to your native struct, and wrap your array in that when marshaling? Really, you’re trying to pass a structure anyway; the only reason for the custom marshaler is to magically generate that struct from the array. Better to avoid the magic, IMO, and at the same time let the default marshaler do the grunt work.