简体   繁体   中英

Using the PropertyDescriptor can I determine if a property is overridden in the current class

If I have:

class A
{
    public virtual string Owner { get; set; }
}

class B : A
{
    public override string Owner { get; set; }
}

How do I determine that owner property on class B is an override property using the TypeDescriptor.GetProperties(type) method?

Based on @DaveShaw's comment and the answers for similar questions using propertyInfo :

var property = TypeDescriptor.GetProperties(typeof(B)).Find("Owner", false).ComponentType.GetProperty("Owner");
var getMethod = property.GetGetMethod(false);
bool isOverride = getMethod.GetBaseDefinition() != getMethod;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM