I have discussed some aspects of property inheritance in my previous posts.

To recap, properties are intrinsically different from methods in that they are simply metadata artifact. You never see them in IL and more importantly to our discussion, they DON'T override each other.

In this third installment I will talk about Type.GetProperties.

Those who are familar with Type.GetMethods already know that if a method on a parent type is overriden in a sub type, GetMethods on the subtype will only return the overriding method, not the overriden one. We also want to mantain this semantics in GetProperties. To do that we need to be able to match a overriding property with the overriden one. In the past we did this by comparing property names and signatures. This worked relatively well untill we introduced generics in V2.

    public class Base<T>

    {

        public virtual T MyProperty { get { return default(T); } }

    }

    public class Derived : Base<int>

    {

        public override int MyProperty { get { return 42; } }

    } 

In the sample code above, the signature of the two MyProperty are T and int respectively and don't match. As a result we don't treat Derived<int>.MyProperty as an override of Base<T>.MyProperty and return both of them in GetProperties.

A better maching criteria is the getter/setter methods. In the same code sample, we can easily determine that Derived<int>.MyProperty does override Base<T>.MyProperty becasue the getter of the latter overrides the getter of the former. This is what we are using in Visual Studio 2010 and .Net Framework 4.0. You can download the beta here: http://www.microsoft.com/visualstudio/en-us/products/2010/default.mspx

Note that Attribute.GetCustomAttributes also uses the same criteria (getters and setters) to find inherited properties.