ICustomMarshaler with IL2CPP

I have a project using a managed plugin which wrap a native one. The wrapper is using the UnmanagedType.CustomMarshaler attribute with MarshalTypeRef to marshal some custom types using a custom marshaler. On Windows, OS X and Android, it works as expected. On iOS (with IL2CPP), a NotSupportedException is thrown, telling me that the custom types cannot be marshalled (the custom marshaler is never created). My current work-around is to manually call the custom marshaler on iOS.

Is that a known limitation of the current version of IL2CPP ?
Or is it a bug because it should be supported ? (I’ll report a bug if it is)

I’m using Unity 5.1.2 on Windows.

Example :

            [DllImport("__Internal", EntryPoint = "CreateSession", CharSet = CharSet.Ansi)]
            public static extern Int32 CreateSession(
                IntPtr resultRelayer,
                [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(SessionDescriptor.Marshaler))] SessionDescriptor sessionDescriptor,
                UInt32 initialSessionTime);

@jp-boivin

Custom marshalers are not currently supported by IL2CPP. I believe it is possible to do so, but we’ve not seen a need for it yet, so we’ve not implemented this.

Do you use this custom marshaling on iOS with Mono? I’m uncertain now about whether or not we support it there. If so, we should probably support it on IL2CPP.

Ok, thanks. I’ll keep my work-around on iOS then.

I don’t use it on iOS with Mono as ARM64 is not supported by it. But I’ve done a rapid test and yes, Mono 2.x supports custom marshaling on iOS.

@jp-boivin

Thanks for trying this with the Mono scripting backend. Could you submit a bug report via Help > Report a Bug in the editor? That way we can feedback to you when this is corrected, and you can remove your work around.

I’ve submitted a bug report. (#732194)

@jp-boivin

Thanks, I appreciate it!

Can we use it yet?
Wanting to load byte[ ] data to classes with nested structures and zlib compressed data auto uncompressed/compressed…

For anyone else reading this:
I ended up just subclassing BinaryReader/BinaryWriter and adding methods to handle compressed data, decompress it to a byte array open a memory stream feed it to a reader and return it. Kind of irksome but useable and still fast.

Nice reading on comparison of different ways to read/write structs to/from byte[ ]: https://www.genericgamedev.com/general/converting-between-structs-and-byte-arrays/

Unforuntately no, we have not implemented this feature yet. I don’t expect that we will implement it soon.