Bug 41874

Summary: Reflection throws AmbiguousMatchException when calling GetProperty on a class that inherits from a generic base class.
Product: [Mono] Class Libraries Reporter: Daniel Casey <dcasey>
Component: mscorlibAssignee: Marek Safar <masafa>
Severity: blocker CC: brendan.zagaeski, christian.schwarz, jonp, luis.aguilera, masafa, mono-bugs+mono, mono-bugs+monodroid, mono-bugs+runtime, peter.collins, riemannzetazero
Priority: ---    
Version: 4.4.0 (C7)   
Target Milestone: (C7SR0)   
Hardware: PC   
OS: Windows   
Tags: BZRC7S1_C6SR4S1, DefectEscapeReview_Done Is this bug a regression?: Yes
Last known good build: Cycle 6 – Service Release 4

Description Daniel Casey 2016-06-16 01:33:46 UTC
# Steps to reproduce
1) Create a new Android project and set up the following classes.

public class TestClass : BaseClass<object>
    public override List<object> TestProperty { get; set; }

public abstract class BaseClass<T>
    public virtual List<T> TestProperty { get; set; }

2) Run the following line of code:
var prop = typeof(TestClass).GetProperty("TestProperty");

# Expected behavior
PropertyInfo should be returned by GetProperty.

# Actual behavior
AmbiguousMatchException is thrown.

# Supplemental info (logs, images, videos)
Tested this code on Cycle 6 Android and in a Windows Console App, an exception is not thrown.  This problem is new to Cycle 7.

The underlying problem is affecting our usage of packages that rely on reflection such as SQLiteNetExtensions and Newtonsoft.Json.

# Test environment (full version information)
Visual Studio 2015 Enterprise (Version 14.0.25123.00 Update 2)
Windows 7

Tested on Android 4.2, 4.3 and 5.0.
Comment 1 Jonathan Pryor 2016-06-16 01:37:57 UTC
This also happens with desktop Mono 4.4:

  using System;
  using System.Collections.Generic;
  public class TestClass : BaseClass<object>
    public override List<object> TestProperty { get; set; }
  public abstract class BaseClass<T>
    public virtual List<T> TestProperty { get; set; }
  class App {
     static void Main ()
       var prop = typeof(TestClass).GetProperty("TestProperty");
       Console.WriteLine (prop);

> $ mono --debug bxc-41874.exe
> Unhandled Exception:
> System.Reflection.AmbiguousMatchException: Ambiguous match found.
>   at System.RuntimeType.GetPropertyImpl (System.String name, BindingFlags bindingAttr, System.Reflection.Binder binder, System.Type returnType, System.Type[] types, System.Reflection.ParameterModifier[] modifiers) [0x000ac] in /private/tmp/source-mono-4.4.0-c7-baseline/bockbuild-mono-4.4.0-c7-baseline/profiles/mono-mac-xamarin/build-root/mono-x86/external/referencesource/mscorlib/system/rttype.cs:3317 
>   at System.Type.GetProperty (System.String name) [0x00011] in /private/tmp/source-mono-4.4.0-c7-baseline/bockbuild-mono-4.4.0-c7-baseline/profiles/mono-mac-xamarin/build-root/mono-x86/external/referencesource/mscorlib/system/type.cs:728 
>   at App.Main () <0x6bfec8 + 0x00023> in <filename unknown>:0 

Perhaps this is part of referencesource migration?
Comment 2 Daniel Casey 2016-06-16 01:43:06 UTC
Considering the Mono 4.4 release notes say "Major upgrade to Reflection APIs, it implements many features that we never used in the past, but that are required to run Roslyn" it seems likely to be to do with that. It is nonetheless a breaking issue.
Comment 3 Marek Safar 2016-06-17 11:53:26 UTC
*** Bug 41917 has been marked as a duplicate of this bug. ***
Comment 4 Marek Safar 2016-06-20 10:55:50 UTC
Fixed in master and Mono 4.5.1

Commit 1d73474cbc93d3f908891fc594c46529d80e2584
Comment 5 Marek Safar 2016-06-20 13:25:47 UTC
Proposed for 4.4 / ff15b42140e1769361ce1f47411f86fc862bb8ba
Comment 6 Peter Collins 2016-06-20 15:37:19 UTC
@Marek is https://github.com/mono/mono/commit/081781dd93463f369922475b3c19dd1170579240 also needed/wanted as part of this fix?
Comment 7 Marek Safar 2016-06-20 15:38:23 UTC
@peter: no, the fix was squashed
Comment 8 Christian Schwarz 2016-06-20 15:59:01 UTC
Do you have any rough estimation when this will land as a Xamarin update?
Comment 9 Peter Collins 2016-06-20 16:26:56 UTC
@Christian the timeline is currently unknown, however we are attempting to land this in our next service release which will hopefully be available at some point this week.

The test case provided no longer throws when running against mono-4.4.0-branch/ff15b42.
Comment 10 Christian Schwarz 2016-06-20 17:39:00 UTC
@Peter thank you for the info, looking forward to it.
Comment 11 Peter Collins 2016-06-20 17:52:34 UTC
Applied to monodroid/c7sr0/7db2aac379d451ca08ebf968a715f32c2341afaf and monodroid/cycle7/e3a462cc3a0f63ccb593efd32ee7916e140f2c5d.
Comment 12 riemannzetazero 2016-06-22 16:25:21 UTC
A console project that produces
- (correctly) one property in .Net 4.5 on Windows
- (incorrectly) two properties in mono 4.4.0 on Ubuntu
Comment 13 Brendan Zagaeski (Xamarin Team, assistant) 2016-06-22 23:10:26 UTC
## Regression and verification status for the test case (from Comment 0): Regression in "Cycle 7" that is now fixed in "Cycle 7 – Service Release 0".

Since there wasn't an explicit mention of the regression status on this bug yet, I am adding one now for thorough record keeping (along with a re-verification of the fix for good measure):

GOOD: Mono 4.4.1 (4747417) "Cycle 7 – Service Release 0"
BAD:  Mono 4.4.0 (5995f74) "Cycle 7"
GOOD: Mono 4.2.4 (71b88f3) "Cycle 6 – Service Release 4"