Repository navigation
Description
Description
On Mono, Type.DefaultBinder.SelectMethod throws NotImplementedException when a candidate is a non-runtime MethodInfo or ConstructorInfo subclass (one not created by the runtime itself, as RuntimeMethodInfo is). The binder calls the internal MethodBase.GetParametersAsSpan(), and Mono's default implementation calls GetParametersInternal(), which throws unless the class overrides it. CoreCLR and NativeAOT default to the public GetParameters().
This reaches users through reflection providers built on MethodInfo subclasses. For example, on a type mapped by a CustomReflectionContext, GetMethod(name, [type]) for the setter of a property the context adds throws on Mono, because the context passes its own accessor to the binder.
In main at be8eaf9, Mono, MethodBase.Mono.cs:42-50:
internal virtual ReadOnlySpan<ParameterInfo> GetParametersAsSpan()
{
return GetParametersInternal();
}
internal virtual ParameterInfo[] GetParametersInternal()
{
throw new NotImplementedException();
}CoreCLR, MethodBase.CoreCLR.cs:57:
internal virtual ReadOnlySpan<ParameterInfo> GetParametersAsSpan() { return GetParameters(); }NativeAOT, MethodBase.NativeAot.cs:20:
public virtual ReadOnlySpan<ParameterInfo> GetParametersAsSpan() => GetParameters();Reproduction Steps
This test in DefaultBinderTests (System.Reflection.Tests), where binder is Type.DefaultBinder, flags is Public | Instance | Static, and MockParameterInfo is the test helper from src/libraries/Common/tests/System/Reflection/MockParameterInfo.cs:
[Fact]
public static void SelectMethod_NonRuntimeMethods()
{
MethodInfo intMethod = new NonRuntimeMethodInfo(typeof(int));
MethodInfo stringMethod = new NonRuntimeMethodInfo(typeof(string));
Assert.Same(stringMethod, binder.SelectMethod(flags, [intMethod, stringMethod], [typeof(string)], null));
Assert.Same(intMethod, binder.SelectMethod(flags, [intMethod, stringMethod], [typeof(int)], null));
Assert.Null(binder.SelectMethod(flags, [intMethod, stringMethod], [typeof(double)], null));
}
private sealed class NonRuntimeMethodInfo(Type parameterType) : MethodInfo
{
private readonly ParameterInfo[] _parameters = [new NonRuntimeParameterInfo(parameterType)];
public override string Name => "Method";
public override Type DeclaringType => typeof(DefaultBinderTests);
public override Type ReflectedType => typeof(DefaultBinderTests);
public override MethodAttributes Attributes => MethodAttributes.Public;
public override RuntimeMethodHandle MethodHandle => throw new NotSupportedException();
public override ICustomAttributeProvider ReturnTypeCustomAttributes => throw new NotSupportedException();
public override ParameterInfo[] GetParameters() => (ParameterInfo[])_parameters.Clone();
public override MethodImplAttributes GetMethodImplementationFlags() => MethodImplAttributes.IL;
public override MethodInfo GetBaseDefinition() => this;
public override object[] GetCustomAttributes(bool inherit) => [];
public override object[] GetCustomAttributes(Type attributeType, bool inherit) => [];
public override bool IsDefined(Type attributeType, bool inherit) => false;
public override object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, Globalization.CultureInfo culture) => throw new NotSupportedException();
}
private sealed class NonRuntimeParameterInfo(Type parameterType) : MockParameterInfo
{
public override Type ParameterType => parameterType;
}Expected behavior
The binder selects the candidate whose parameter type matches, as on CoreCLR and NativeAOT.
Actual behavior
On Mono: System.NotImplementedException : The method or operation is not implemented. at System.Reflection.MethodBase.GetParametersInternal().
Regression?
No. release/8.0 has the same code under the earlier name GetParametersNoCopy(), renamed in #92310.
Known Workarounds
Overriding GetParameters() doesn't help, since the binder doesn't call it on Mono. A reflection provider can match parameter types itself instead of calling Type.DefaultBinder.
Configuration
main at be8eaf9, Mono Debug, Windows x64.
Other information
Proposal: make Mono's MethodBase.GetParametersInternal() default return GetParameters(), matching CoreCLR and NativeAOT. RuntimeMethodInfo, RuntimeConstructorInfo, MonoArrayMethod, RuntimeMethodBuilder and RuntimeConstructorBuilder override it (DynamicMethod overrides GetParametersAsSpan()); none of their GetParameters() calls the default. Three shared classes (ConstructorOnTypeBuilderInstantiation, MethodBuilderInstantiation, MethodOnTypeBuilderInstantiation) don't override it either; with the change they would return their GetParameters(), which forwards to the wrapped member.
The change and test above are ready. Given #134397 (removing the .NET 12 Mono runtime packs), I'll open a PR if a Mono fix is still wanted. Could this be assigned to me?
Note
AI-generated, written at my direction and reviewed by me before posting. The test above ran on a local Debug Mono build of main (Windows x64, JIT) and failed as described; it passes on CoreCLR. The CustomReflectionContext case ran on browser-wasm with a related Reflection.Context change applied; on main it reaches the same binder call by reading. Code excerpts are from main at be8eaf9; the regression answer comes from reading release/8.0, not from running it.
Activity