Skip to content

monovm & Type.GetCustomAttributes() doesn't like interfaces #33639

Description

@jonpryor

Context: #7190
Context: https://gist.github.com/pjcollins/a3cdf82a6e04cfafbb0ea613b769a8bb
Context: https://github.com/xamarin/java.interop/blob/bd7c60a6a5ea8abaf6f241da17c42d2d93226bf2/src/Java.Interop.Tools.TypeNameMappings/Java.Interop.Tools.TypeNameMappings/JavaNativeTypeManager.cs#L291
Context: https://github.com/xamarin/xamarin-android/tree/test-net5-drop

The attempt to run Xamarin.Android atop monovm has run into a compatibility issue around Type.GetCustomAttributes() and interfaces.

Consider the following code (philosophically similar to Issue #7190 but without external dependencies):

using System;

interface INameable {
    string Name {get;}
}

[AttributeUsage (AttributeTargets.All, AllowMultiple=true)]
class AAttribute : Attribute, INameable {
    string INameable.Name {
        get => "A";
    }
}

[AttributeUsage (AttributeTargets.All, AllowMultiple=true)]
class BAttribute : Attribute, INameable {
    string INameable.Name {
        get => "B";
    }
}

[A, B]
class Example {
}

class App {
    public static void Main ()
    {
        var attributes = (INameable[]) typeof(Example).GetCustomAttributes(typeof(INameable), inherit: false);
        Console.WriteLine ($"Found {attributes.Length} attributes");
        foreach (var attribute in attributes) {
            Console.WriteLine (attribute.Name);
        }
    }
}

The above works on desktop mono and dotnet run, but fails with the .NET 5 monovm, along the lines of this gist:

System.ArgumentException: Type passed in must be derived from System.Attribute or System.Attribute itself. INameable
   at System.Reflection.CustomAttribute.GetCustomAttributes(ICustomAttributeProvider obj, Type attributeType, Boolean inherit)
   at System.RuntimeType.GetCustomAttributes(Type attributeType, Boolean inherit)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    runtime-monospecific to the Mono runtime

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions