gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:27:26 25 4
gpt4 key购买 nike

如果我有:

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

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

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

最佳答案

基于@DaveShaw 的评论和使用 propertyInfo 对类似问题的回答:

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

关于c# - 使用 PropertyDescriptor 可以确定属性是否在当前类中被覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25415719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com