mono / mono

Mono open source ECMA CLI, C# and .NET implementation.
https://www.mono-project.com
Other
11.13k stars 3.82k forks source link

GetCustomAttribute on inherited property throws IndexOutOfRangeException #17477

Open Steve887 opened 5 years ago

Steve887 commented 5 years ago

Description

I ran into an interesting situation trying to get Entity Framework working in my Xamarin application which I have narrowed down to what seems to be a bug internally. This may be the wrong place to post this as it may be better on the AspNetCore repo so I will move if required.

The bug occurs when you call GetCustomAttribute on an inherited property's GetMethod ReturnParameter. This results in an IndexOutOfRangeException. However, if the same code is called from a netcoreapp3.0 application it works as expected.

The exception occurs inside the internal InternalParamGetCustomAttributes method.

The setup for this reproduction comes from Entity Framework Core's NonNullableConventionBase which is called during Model setup, but Entity Framework is not required to reproduce.

var works = typeof(Blog).GetProperty("Url").GetMethod.ReturnParameter.GetCustomAttribute(typeof(AttributeUsageAttribute));
var worksAlso = typeof(Blog).GetProperty("Id").GetMethod.ReturnParameter.GetCustomAttribute(typeof(AttributeUsageAttribute));
var fails = typeof(Blog).GetProperty("Author").GetMethod.ReturnParameter.GetCustomAttribute(typeof(AttributeUsageAttribute));

public class Blog : BaseBlog
{
    public int Id { get; set; }
    public string Url { get; set; }
}

public class BaseBlog
{
    public string Author { get; set; }
}

Steps to Reproduce

  1. Call GetMethod.ReturnParameter.GetCustomAttribute on an inherited property.

Expected Behavior

The Custom Attribute is returned, or null if it doesn't exist.

Actual Behavior

System.IndexOutOfRangeException

Basic Information

Screenshots

image

image

Reproduction Link

IndexOutOfRangeException.zip Switch startup project between the Console app and Android app to see the issue.

mindphaser commented 4 years ago

Got this issue today when tried to migrate EF Core from 2.2.6 to 3.0.0 in Xamarin Forms project.

Steve887 commented 4 years ago

@mindphaser That's how I came across the issue as well. It appears the EF Core team has worked around the issue in the latest preview build 3.1.0-preview2.19525.5 with this commit: https://github.com/aspnet/EntityFrameworkCore/commit/5fec0d085ef7e5702208c1169758f4f2d4e996d7#diff-920a9f983bb40645fe12f779d7f2d6df

The NonNullableConventionBase has been updated to do it's own filtering of Custom Attributes, rather than use the internal GetCustomAttribute implementation.

Steve887 commented 4 years ago

@mindphaser I opened an issue in EFCore to track https://github.com/aspnet/EntityFrameworkCore/issues/18793

MaximMikhisor commented 4 years ago

Got this issue today when tried to migrate EF Core from 3.1.7 to 5.0.0-rc.1.20451.13 in Xamarin Forms project.