c# - 有没有办法从继承的属性中删除属性?

标签 c# data-annotations

是否可以从继承的属性中删除属性?我认为通过使用 new 关键字我可以这样做...

 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 FirstName { get; set; }
 }

...但这根本不起作用。这让我感到惊讶,因为 new 专门用来隐藏继承的成员。

最佳答案

您的 Employee 类现在有 2 个 FirstName 属性,其中一个仍然是 [Required] ...

直接回答:不,据我所知,您不能删除属性。那将违反替代原则。当 Employee IS-A Person 时,Person.FirstName 的属性适用。

并且:此处的 new 关键字用于抑制“X is hiding base class member..”警告。它对代码的语义没有任何影响。

关于c# - 有没有办法从继承的属性中删除属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3367104/

相关文章:

asp.net-mvc - 在 ASP.net MVC 中通过 jQuery 在客户端本地化验证消息

c# - 效率 : Creating an array of doubles incrementally?

c# - 将文本框中的信息发布到不同解决方案中的另一个文本框

c# - 在一个类中拥有多个锁对象的最佳实践

c# - 如何获取当前 View 的url?

c# - 合并msi和exe

c# - 自定义数据注释(数据类型)

.net - MVC中UIHint属性有什么用

c# - RequiredIf 没有触发客户端验证

.net - 使用 xsd.exe 从程序集中的类型生成 XSD,在属性上具有数据注释属性