-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Description
There is a difference between Mono & .NET Framework/.NET Core regarding the use of Marshal.GetFunctionPointerForDelegate()
and generic delegate types. Consider:
using System;
using System.Runtime.InteropServices;
class App {
public static void Main ()
{
Action<int> a = v => {};
var p = Marshal.GetFunctionPointerForDelegate (a);
}
}
On .NET Framework, this app fails:
Unhandled Exception: System.ArgumentException: The specified Type must not be a generic type definition.
Parameter name: delegate
at System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegateInternal(Delegate d)
at App.Main()
This works on Mono.
.NET Core currently has the same check as .NET Framework:
runtime/src/coreclr/src/vm/comdelegate.cpp
Line 1228 in fcd862e
COMPlusThrowArgumentException(W("delegate"), W("Argument_NeedNonGenericType")); |
Rationale: Xamarin.Android currently uses System.Action<...>
and System.Func<...>
with Marshal.GetFunctionPointerForDelegate()
to register function pointers with JNI. This has always worked on Mono, and Xamarin.Android uses mono, so it's been Fine.
However, it would be nice to use Xamarin.Android's JNI infrastructure on .NET Core. At present, if this were to be done it would fail as soon as we hit Marshal.GetFunctionPointerForDelegate()
for method registration.
The Xamarin.Android team could instead alter their binding infrastructure so that Action<...>
and Func<...>
are not used. The Xamarin.Android team would like to know if this code generator change is required for eventual .NET Core support, or if we can instead forego this change.
Thanks to the Similar issues window, #4547 was suggested. I'm not sure if this is entirely duplicative or not, but Issue #4547 is currently Closed, though it was also added to the .NET 5 milestone, so I'm not entirely sure if these are the same or not.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status