开发者

Is there a way to remove attributes from an inherited property?

开发者 https://www.devze.com 2023-01-09 17:21 出处:网络
Is it possible to remove attributes from inherited properties?I thought that by using the new keyword I could do so...

Is it possible to remove attributes from inherited properties? I thought that by using the new keyword I could do so...

 public class Person
 {
     [Required]
     public string FirstName { get; set; }

     [Required]
     public string LastName { get; set; }
 }

 public class Employee : Person
 {
     [Required]
     public string JobTitle { get; set; }

     public new string Fi开发者_开发百科rstName { get; set; }
 }

... but this doesnt work at all. This surprises me because the new is specifically there to hide inherited members.


Your Employee class now has 2 FirstName properties, one of them is still [Required] ...

Direct answer: No, you cannot remove attributes for as far as I know. That would violate the substitution principle. When an Employee IS-A Person then the properties of Person.FirstName apply.

And: the new keyword here only serves to suppress the 'X is hiding base class member..' warning. It has no effect whatsoever on the semantics of your code.

0

精彩评论

暂无评论...
验证码 换一张
取 消