Repository navigation
Description
Description
On Mono, MethodInfo.CreateDelegate and Delegate.CreateDelegate create a delegate over a static abstract interface method, which has no body; calling the delegate throws BadImageFormatException: Method has no body.
The by-name overloads (Delegate.CreateDelegate(Type, Type, string, ...)) bind such a method too.
PropertyInfo.GetValue on I.P below throws TargetInvocationException wrapping the same BadImageFormatException: Mono calls the getter through a delegate of this kind.
PR #135566 makes CoreCLR and NativeAOT throw MemberAccessException when such a delegate is created, and makes their by-name overloads skip the method. Mono's code for these paths is separate, and the PR doesn't change it.
Reproduction Steps
using System;
using System.Reflection;
interface I
{
static abstract void M();
static abstract int P { get; }
}
static class Program
{
static void Main()
{
MethodInfo m = typeof(I).GetMethod(nameof(I.M))!;
Run("MethodInfo.CreateDelegate", () => m.CreateDelegate<Action>());
Run("Delegate.CreateDelegate by name", () => Delegate.CreateDelegate(typeof(Action), typeof(I), nameof(I.M)));
Run("Call the delegate", () => { m.CreateDelegate<Action>()(); return null; });
Run("PropertyInfo.GetValue", () => typeof(I).GetProperty(nameof(I.P))!.GetValue(null));
}
static void Run(string name, Func<object?> action)
{
try
{
Console.WriteLine($"{name}: returned {action()?.GetType().Name ?? "null"}");
}
catch (Exception e)
{
string inner = e.InnerException is Exception i ? $" -> {i.GetType().Name}: {i.Message}" : $": {e.Message}";
Console.WriteLine($"{name}: {e.GetType().Name}{inner}");
}
}
}Run it on the Mono runtime.
Expected behavior
As on CoreCLR with #135566:
MethodInfo.CreateDelegate: MemberAccessException: Cannot bind a delegate to a static abstract method.
Delegate.CreateDelegate by name: ArgumentException: Cannot bind to the target method because its signature is not compatible with that of the delegate type.
Call the delegate: MemberAccessException: Cannot bind a delegate to a static abstract method.
PropertyInfo.GetValue: MemberAccessException: Cannot call static abstract method 'I.get_P'.
Actual behavior
MethodInfo.CreateDelegate: returned Action
Delegate.CreateDelegate by name: returned Action
Call the delegate: BadImageFormatException: Method has no body
PropertyInfo.GetValue: TargetInvocationException -> BadImageFormatException: Method has no body
The same output on the Mono JIT and the interpreter.
Regression?
No. The released Mono runtime 8.0.28 (win-x64) prints the same.
Known Workarounds
Test IsStatic && IsAbstract on the method first: for GetValue, on the property's GetMethod; for a by-name call, on the method Type.GetMethod finds.
Configuration
dotnet/runtime main at ec0059c with #135566 applied, Mono built with build.cmd mono+libs -rc checked; Windows 11, x64. Also the released Mono 8.0.28 win-x64 runtime.
Other information
ves_icall_System_Delegate_CreateDelegate_internal checks the method only for a generic method definition before creating the delegate:
if (sig->generic_param_count && method->wrapper_type == MONO_WRAPPER_NONE) {
if (!method->is_inflated) {
mono_error_set_argument (error, "method", " Cannot bind to the target method because its signature differs from that of the delegate type");
return NULL_HANDLE;
}
}The by-name lookup returns the first static method that matches the name and signature:
for (Type? targetType = target; targetType != null; targetType = targetType.BaseType)
{
MethodInfo? mi = targetType.GetMethod(method, flags, null, delargtypes, Array.Empty<ParameterModifier>());
if (mi != null && IsReturnTypeMatch(invoke.ReturnType!, mi.ReturnType!))
{
return mi;
}
}For I.P, RuntimePropertyInfo.GetValue builds a delegate over the getter (CreateGetterDelegate, RuntimePropertyInfo.cs:411) and calls it.
#135566's tests for this (MethodInfoTests.CreateDelegate_StaticAbstract_ThrowsMemberAccessException, CreateDelegate_EmittedStaticAbstract_ThrowsMemberAccessException, CreateDelegate_ByName_StaticAbstract_DoesNotBind, PropertyInfoTests.GetValue_StaticAbstract_ThrowsMemberAccessException) skip Mono and will reference this issue.
A delegate closed over the first argument of a static abstract method aborts the Mono JIT when the argument's class doesn't implement the interface (#136097).
I can implement a fix, with tests, if it is wanted. Could this be assigned to me?
Activity
dotnet-policy-service commentedon Oct 26, 2026
Tagging subscribers to this area: @steveisok, @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.
Skip known Mono bugs with ActiveIssue