Bug 36305 - Method.GetBaseDefinition() differs from .Net when generics are involved
Summary: Method.GetBaseDefinition() differs from .Net when generics are involved
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: Reflection ()
Version: 4.2.0 (C6)
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Aleksey Kliger
URL:
Depends on:
Blocks:
 
Reported: 2015-11-27 06:31 UTC by Kalyanov.Dmitry
Modified: 2015-12-10 16:15 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Kalyanov.Dmitry 2015-11-27 06:31:19 UTC
Here's a program:

```
using System;
class Program
{
    static void Main(string[] args)
    {
        var t1 = typeof(G3).GetMethod("Foo").DeclaringType;
        var t2 = typeof(G3).GetMethod("Foo").GetBaseDefinition().DeclaringType;
        Console.WriteLine(t1.FullName);
        Console.WriteLine(t2.FullName);
        Console.WriteLine(t1.IsAssignableFrom(typeof(G3)));
        Console.WriteLine(t2.IsAssignableFrom(typeof(G3)));
    }
}

public class G<O, T> { public virtual void Foo() { } }
public class G2<O> : G<O, string> { public override void Foo() { } }
public class G3 : G2<string> { }
```

On mono 4.2 it prints:
```
G2`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
G`2[O,[System.String, mscorlib, VersionCulture=neutral, PublicKeyToken=b77a5c561934e089]]
True
True
```

On .net 4.5 it prints:
```
G2`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
G`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, VersionCulture=neutral, PublicKeyToken=b77a5c561934e089]]
True
False
```

I think that mono's behavior is wrong here.

According to MSDN (https://msdn.microsoft.com/ru-ru/library/system.reflection.methodinfo.getbasedefinition(v=vs.110).aspx):

> returns the MethodInfo object for the method on the direct or indirect base class
> in which the method represented by this instance was first declared

But clearly t2 is not the base class for G3 (it's confirmed by `t2.IsAssignableFrom(typeof(G3)) == false`).
Comment 1 Andi McClure 2015-12-07 18:09:48 UTC
Tested on f1c147af8f1 64-bit on OS X El Capitan. Does not reproduce.
Comment 2 Andi McClure 2015-12-07 18:37:40 UTC
Tested on mono-4.2.1.102, 64-bit, OS X El Capitan. Does not reproduce. When I run your test program, I get:

G2`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
G`2[O,[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
True
False

Which what you are describing as "correct"/.NET behavior. Please retest with the newest stable Mono and if you are still seeing the problem, reopen and specify the exact version, operating system, and arch (32/64 bit) where you are seeing it.
Comment 3 Aleksey Kliger 2015-12-07 19:49:49 UTC
We agree on isAssignableFrom(), but Mono still gives a different result for .GetBaseDefinition().DeclaringType  (second line of output... note that .NET says the first type argument is String, and we say it's O)
Comment 4 Aleksey Kliger 2015-12-09 14:43:11 UTC
Mono does the wrong thing in GetBaseDefinition() when a base class is an open constructed type (such as G<O, string>, the base class of G2<O>, above).  The right thing to do is to apply the type instantiations as we work our way up the class hierarchy.
Comment 5 Aleksey Kliger 2015-12-10 16:15:01 UTC
Fixed in master ca39924db549a69fe6b7dc9ab05137e0ad13afe6