Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jonpryor opened this issue Mar 16, 2020 · 3 comments · Fixed by #33942
Closed

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

jonpryor opened this issue Mar 16, 2020 · 3 comments · Fixed by #33942
Labels
runtime-mono specific to the Mono runtime
Milestone

Comments

@jonpryor
Copy link
Member

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)
@lambdageek
Copy link
Member

/cc @CoffeeFlux

@CoffeeFlux
Copy link
Contributor

This was caused by my commit here: 0844cb7a6152

I'd hoped to simplify the type checking, but I broke this in the process. With that said, I think this is only a problem for interfaces, so I should be able to special-case those and move along without undoing my changes? I'll also add a test to libraries so we don't regress on this again.

@marek-safar
Copy link
Contributor

/cc @vitek-karas

@ghost ghost locked as resolved and limited conversation to collaborators Dec 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
runtime-mono specific to the Mono runtime
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants