Property hiding and reflection (C#)
- by tehMick
Declaring a property in a derived class that matches the name of a property in the base class "hides" it (unless it overrides it with the override keyword). Both the base and derived class properties will be returned by Type.GetProperties() if their types don't match. However, if their types do match, shockingly only the derived class's property is returned. For instance:
class A
{
protected double p;
public int P { get { return (int)p; } set { p = value; } }
}
class B : A
{
public new int P { get { return (int)p; } set { p = value; } }
}
class C : B
{
public new float P { get { return (float)p; } set { p = value; } }
}
Calling typeof(C).GetProperties() will only return B.P and C.P. Is it possible to call GetProperties() in a way that returns all three? There is almost certainly a way to do it by traversing the inheritance hierarchy, but is there a cleaner solution?