How to add attributes to a base class's properties

后端 未结 4 1925
独厮守ぢ
独厮守ぢ 2020-12-08 12:51

I have a couple model classes like so:

public class MyModelBase
{
    public string Name { get; set; }
}

public class MyModel : MyModelBase
{
    public str         


        
4条回答
  •  执笔经年
    2020-12-08 13:36

    I note that none of these answers actually call the base Name property correctly. The override should write something like the following, in order that you don't have a separate value for the new property.

    public class MyModelBase
    {
        public virtual string Name { get; set; }
    }
    
    public class MyModel : MyModelBase
    {
        [Required]
        public override string Name { get { return base.Name; } set { base.Name = value; }
    
        public string SomeOtherProperty { get; set; }
    }
    

提交回复
热议问题