Example #1
0
        static Func <IntPtr, JniHandleOwnership, object> GetJniHandleConverter(Type target)
        {
            if (target == null)
            {
                return(null);
            }

            Func <IntPtr, JniHandleOwnership, object> converter;

            if (JniHandleConverters.TryGetValue(target, out converter))
            {
                return(converter);
            }
            if (target.IsArray)
            {
                return((h, t) => JNIEnv.GetArray(h, t, target.GetElementType()));
            }
            if (target.IsGenericType && target.GetGenericTypeDefinition() == typeof(IDictionary <,>))
            {
                Type t = typeof(JavaDictionary <,>).MakeGenericType(target.GetGenericArguments());
                return(GetJniHandleConverterForType(t));
            }
            if (typeof(IDictionary).IsAssignableFrom(target))
            {
                return((h, t) => JavaDictionary.FromJniHandle(h, t));
            }
            if (target.IsGenericType && target.GetGenericTypeDefinition() == typeof(IList <>))
            {
                Type t = typeof(JavaList <>).MakeGenericType(target.GetGenericArguments());
                return(GetJniHandleConverterForType(t));
            }
            if (typeof(IList).IsAssignableFrom(target))
            {
                return((h, t) => JavaList.FromJniHandle(h, t));
            }
            if (target.IsGenericType && target.GetGenericTypeDefinition() == typeof(ICollection <>))
            {
                Type t = typeof(JavaCollection <>).MakeGenericType(target.GetGenericArguments());
                return(GetJniHandleConverterForType(t));
            }
            if (typeof(ICollection).IsAssignableFrom(target))
            {
                return((h, t) => JavaCollection.FromJniHandle(h, t));
            }

            return(null);
        }