Property hiding and reflection (C#)
Posted
by tehMick
on Stack Overflow
See other posts from Stack Overflow
or by tehMick
Published on 2010-04-26T16:16:42Z
Indexed on
2010/04/28
18:17 UTC
Read the original article
Hit count: 181
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?
© Stack Overflow or respective owner