Throw InvalidOperationException on RuntimeMethodHandle.GetFunctionPointer() for generic methods#104644
Conversation
…nter() for generic methods
|
@steveharter what should this do: public class G<T>
{
public static T Method(T x) => x;
}
public class Program
{
public static void Main()
{
var mi = typeof(G<>).GetMethod("Method", BindingFlags.Public|BindingFlags.Static);
var h = mi.MethodHandle;
var fp = h.GetFunctionPointer();
Console.WriteLine(fp);
}
} On CoreCLR this works (returns some kind of non-null value for (same behavior on .NET 9 preview 6 and .NET 8, across both runtimes) |
src/mono/mono/mini/mini-runtime.c
Outdated
| static gpointer | ||
| get_ftnptr_for_method (MonoMethod *method, gboolean need_unbox, MonoError *error) | ||
| { | ||
| if (method->is_generic) { |
There was a problem hiding this comment.
if we want to also rule out methods from a generic type definition, then you may want to add || mono_class_is_gtd (method->klass)
There was a problem hiding this comment.
Yes we should throw if the method is not callable meaning a "true" generic method and a generic method that uses the type parameters from the owning class. It seems like IsGenericMethod() should return true even if the type parameters come from the owning class, but that's a different problem...
I'll make another pass here. Thanks
|
Merging as @steveharter is out |
Fixes #101664
On CoreClr, the previous exception was InvalidProgramException. On Mono, the call caused a crash.