Use reflection to work around CustomMarshaler bug

Fixes #241