是否可以在C#中继承数据注释?

是否可以在C#中继承数据注释?,c#,data-annotations,C#,Data Annotations,我可以在另一个类中继承“password”数据注释吗 public class AccountCredentials : AccountEmail { [Required(ErrorMessage = "xxx.")] [StringLength(30, MinimumLength = 6, ErrorMessage = "xxx")] public string password { get; set; } } 另一类: public class Pa

我可以在另一个类中继承“password”数据注释吗

    public class AccountCredentials : AccountEmail
{
    [Required(ErrorMessage = "xxx.")]
    [StringLength(30, MinimumLength = 6, ErrorMessage = "xxx")]
    public string password { get; set; }
}
另一类:

    public class PasswordReset : AccountCredentials
{
    [Required]
    public string resetToken { get; set; }
    **["use the same password annotations here"]**
    public string newPassword { get; set; }
}
由于API调用的原因,我不得不使用不同的模型,但我希望避免为同一个字段维护两个定义。 谢谢

附加:类似于

[UseAnnotation[AccountCredentials.password]]
public string newPassword { get; set; }

在基类中,可以将其设置为
virtual
属性,并在派生类中将其更改为
override
。但是,它不会继承属性,我们在这里做了一件棘手的事情:

public class AccountCredentials : AccountEmail
{
 [Required(ErrorMessage = "xxx.")]
 [StringLength(30, MinimumLength = 6, ErrorMessage = "xxx")]
 public virtual string password { get; set; }
}

public class PasswordReset : AccountCredentials
{
 [Required]
 public string resetToken { get; set; }
 public override string password { get; set; }
}
考虑并使用


也许它已经成为我的一把金锤,但最近我在这方面取得了很多成功,特别是在创建一个基类或者取而代之的是将共享行为封装到一个对象中时。继承可以很快失去控制

如果你定义了一个全新的属性——你认为evn在哪里可以神奇地继承?那不是继承,那是魔法。编译器神奇地知道一个新属性与一个旧属性相关。对不起,也许我没有很好地定义它。我倒是想,会不会有类似于[使用[AccountCredentials.passwordAnnotation]]的东西,他引用的是
AccountCredentials
中的属性是
password
,您希望新类
PasswordReset
的属性
newPassword
AccountCredential
password
属性继承数据。所以他问你,当你在第二个类中定义一个全新的属性时,是什么让你认为继承适用。
    public class AccountEmail { }

    public class AccountCredentials : AccountEmail
    {
        public Password Password { get; set; }
    }

    public class PasswordReset : AccountCredentials
    {
        [Required]
        public string ResetToken { get; set; }

        public Password NewPassword { get; set; }
    }

    public class Password
    {
        [Required(ErrorMessage = "xxx.")]
        [StringLength(30, MinimumLength = 6, ErrorMessage = "xxx")]
        public string Value { get; set; }

        public override string ToString()
        {
            return Value;
        }
    }