GetCustomAttributes(true) 不从类型父级返回属性?
我有以下(大大简化的)类:
public class Customer {
[Required(ErrorMessageResourceName="Required", ErrorMessageResourceType=typeof(ResourcesCommon.ValidationStrings))]
public string LastName { get; set; }
}
public class SalesCustomer : Customer {
...
}
我有一些循环遍历 SalesCustomer 属性的验证代码。每个属性都按以下方式进行评估:
var validators = property.GetCustomAttributes(typeof(ValidationAttribute), true);
问题是,当我查看 SalesCustomer 类型的 LastName 属性时,未返回必需属性,但当我查看 Customer 类型时,它确实工作正常。这很令人困惑,因为 GetCustomerAttribute() 方法明确需要一个布尔值来指示将返回继承的属性。
I have the following (greatly simplified) classes:
public class Customer {
[Required(ErrorMessageResourceName="Required", ErrorMessageResourceType=typeof(ResourcesCommon.ValidationStrings))]
public string LastName { get; set; }
}
public class SalesCustomer : Customer {
...
}
I have some validation code that loops through SalesCustomer properties. Each property is evaluated with this:
var validators = property.GetCustomAttributes(typeof(ValidationAttribute), true);
The problem is that the Required attribute is not getting returned when I look at the LastName property on the SalesCustomer type but it does work fine when I look at the Customer type. This is confusing since the GetCustomerAttribute() method explicitly wants a boolean indicating that the inherited attributes will be returned.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信它只会返回来自覆盖父成员的属性,而不是隐藏父成员的属性。
将基本属性设置为
虚拟
,并将子属性设置为覆盖
。I believe that it will only return attributes from overridden parent members, not shadowed parent members.
Make the base property
virtual
and the child propertyoverrrides
.