[Mono-list] Custom marshalling

Marcus mathpup@mylinuxisp.com
Fri, 6 Aug 2004 00:46:07 -0500


Unfortunately, UnmanagedMarshal.DefineCustom is a Mono-only class and not part 
of .NET. The normal .NET mechanism is to create a custom attribute using 
CustomAttributeBuilder to obtain an instance of MarshalAsAttribute with the 
custom marshaler specified. That does not seem to work under Mono.

It is possible to implement custom marshalers without having to use 
reflection, as long as you know what what types you want to marshal to what 
at compile time. 

public class MyMarshal: ICustomMarshaler {...}

private static extern void PrintObject(
 [MarshalAs( UnmanagedType.CustomMarshaler, MarshalTypeRef =
        typeof(MyMarshal ) )] object o );

On Sunday 25 July 2004 5:12 pm, Alan Jenkins wrote:
> As far as I can tell, Mono supports custom marshalling outside of function
> calls (like PInvoke),  - ie using Marshal.StructToPtr and PtrToStruct, but
> you have to use UnmanagedMarshal.DefineCustom, which would involve
> reflection of some sort - possibly creating your class with custom
> marshalled fields by reflection.
>
> Does UnmanagedMarshal.DefineCustom work?
> Is it efficient (as far as custom marshalling can be)?
> Is it supported (or undocumented and likely to disappear/break)?
>
> Would it be (technically) possible to access its functionality through an
> attribute, in the same way that non-custom marshalling is done?
> Is this likely to happen?