From ec2813acd69b0a7cc35a1eee5a1f2ee10a57dfff Mon Sep 17 00:00:00 2001 From: Radek Doulik Date: Fri, 26 Oct 2018 18:07:37 +0200 Subject: [PATCH] [Java.Interop] Additional marshaler lookup (#389) Context: https://github.com/xamarin/java.interop/issues/388 Before going to use the proxy object marshaler, try to check the implemented interfaces of `type` for a custom marshaler. It can be used in xamarin-android to marshal `IJavaObject` based objects, which do not implement `IJavaPeerable`. --- .../Java.Interop/JniRuntime.JniValueManager.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs b/src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs index 8c4b27c28..6e7b4a7ea 100644 --- a/src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs +++ b/src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs @@ -527,6 +527,20 @@ public JniValueMarshaler GetValueMarshaler (Type type) if (typeof (IJavaPeerable).GetTypeInfo ().IsAssignableFrom (info)) { return JavaPeerableValueMarshaler.Instance; } + + JniValueMarshalerAttribute ifaceAttribute = null; + foreach (var iface in info.ImplementedInterfaces) { + marshalerAttr = iface.GetTypeInfo ().GetCustomAttribute (); + if (marshalerAttr != null) { + if (ifaceAttribute != null) + throw new NotSupportedException ($"There is more than one interface with custom marshaler for type {type}."); + + ifaceAttribute = marshalerAttr; + } + } + if (ifaceAttribute != null) + return (JniValueMarshaler) Activator.CreateInstance (ifaceAttribute.MarshalerType); + return GetValueMarshalerCore (type); }