Bug 42357 - ReflectedType() returns DeclaringType
Summary: ReflectedType() returns DeclaringType
Alias: None
Product: Android
Classification: Xamarin
Component: Mono runtime / AOT Compiler ()
Version: 6.1.0 (C7)
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
Depends on:
Reported: 2016-07-05 13:13 UTC by Sergey Zhukov
Modified: 2016-07-06 11:22 UTC (History)
1 user (show)

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 Developer Community or GitHub 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:

Description Sergey Zhukov 2016-07-05 13:13:31 UTC
This is regression bug. Test case works correctly on .NET and Xamarin v4.0, but does not work on Xamarin v4.1. Looks similar to bug #12205.

Actual behaviour: when trying to get ReflectedType() on PropertyInfo of derived class it returns base class type instead of derived type.
Expected behaviour: ReflectedType() should be the same as type of derived class

Works on: .NET 4.5, Xamarin (0dd81c) (Xamarin.Android (a94a03b))
Does not work on: Xamarin (2e39740) (Xamarin.Android (4e27558)) and later versions

    public class DPBase
        protected string base64Value = string.Empty;
        public string Value { get { return this.base64Value; }  private set { this.base64Value = value; } }

        public void Set(string val)
            this.base64Value = val;

    public class DP1 : DPBase


    public void Test()
       var dp1 = new DP1();
       var propertyInfo = dp1.GetType().GetPropertyInfo("Value");

       //reflectedType must be of type typeof(DP1), but its value is typeof(DPBase)
       var reflectedType = propertyInfo.ReflectedType();
       //in correct scennario propertyInfo should get new value from the base class but it does not
       if (propertyInfo.ReflectedType() != propertyInfo.DeclaringType)
              propertyInfo = propertyInfo.DeclaringType.GetPropertyInfo(propertyInfo.Name);

Comment 1 Jonathan Pryor 2016-07-05 18:20:38 UTC
Is this using some additional extension methods? I can't get it to compile:

> Error CS1061: Type `System.Type' does not contain a definition for `GetPropertyInfo'
> and no extension method `GetPropertyInfo' of type `System.Type' could be found.
> Are you missing an assembly reference? (CS1061) (Scratch.Bxc42357)

In the off chance you want a `GetTypeInfo()` in there...that doesn't work, as there isn't a TypeInfo.GetPropertyInfo() method either.

Please provide a complete repro.

Perhaps this is a duplicate of Bug #41874?
Comment 2 Sergey Zhukov 2016-07-06 11:22:02 UTC
You are right, I did not post extensions to get PropertyInfo (wraps GetProperty() method) and ReflectedType. 

I close the issue because when I looked close it seemed that bug with reflection was in v4.0, while in v4.1 it works as expected.