From 6552926ea37553837c5d2acad45e07dbb6efc712 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Thu, 9 Apr 2026 09:28:16 +0200 Subject: [PATCH] [Mono.Android] Throw NotSupportedException for open generic type activation JNIEnv.StartCreateInstance(Type, ...) now checks type.IsGenericTypeDefinition and throws NotSupportedException before attempting to allocate the Java object. In the legacy path, this check was performed later by ManagedPeer.Construct during FinishCreateInstance. Moving it earlier ensures both the legacy and trimmable typemap paths fail consistently for open generic types. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/Mono.Android/Android.Runtime/JNIEnv.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Mono.Android/Android.Runtime/JNIEnv.cs b/src/Mono.Android/Android.Runtime/JNIEnv.cs index 8b004855ba8..d09c14c840c 100644 --- a/src/Mono.Android/Android.Runtime/JNIEnv.cs +++ b/src/Mono.Android/Android.Runtime/JNIEnv.cs @@ -170,6 +170,10 @@ public static unsafe void FinishCreateInstance (IntPtr instance, IntPtr jclass, public static unsafe IntPtr StartCreateInstance (Type type, string jniCtorSignature, JValue* constructorParameters) { + if (type.IsGenericTypeDefinition) { + throw new NotSupportedException ( + "Constructing instances of generic types from Java is not supported, as the type parameters cannot be determined."); + } return AllocObject (type); }