使用PropertyDescriptor可以确定属性是否在当前类中被覆盖

本文关键字:覆盖 是否 PropertyDescriptor 属性 使用 | 更新日期: 2023-09-27 18:18:50

如果我有:

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

我如何确定类B上的所有者属性是使用TypeDescriptor.GetProperties(type)方法的重写属性?

使用PropertyDescriptor可以确定属性是否在当前类中被覆盖

基于@DaveShaw的评论和使用propertyInfo:

对类似问题的回答
var property = TypeDescriptor.GetProperties(typeof(B)).Find("Owner", false).ComponentType.GetProperty("Owner");
var getMethod = property.GetGetMethod(false);
bool isOverride = getMethod.GetBaseDefinition() != getMethod;