这是我的父抽象类:
public abstract class PersonViewModel:IPersonViewModel
{
#region DataValidatorModel
[Required(ErrorMessage = "ErrorInsertYourMobileNumber")]
[Display(Name = "MobileNumber")]
//--RegularExpression ========================(1234567890)===!( ValiAsr1)
[RegularExpression(pattern: @"(\d{10})", ErrorMessage = "ErrorNotFindMobileNumber")]
#endregion
public string MobileNumber { get; set; }
#region DataValidatorModel
[Required(ErrorMessage = "ErrorInsertPassword")]
[Display(Name = "Password")]
[DataType(DataType.Password)]
#endregion
public string Password { get; set; }
#region DataValidatorModel
[Required(ErrorMessage = "ErrorInsertReTypePassword")]
[Display(Name = "ReTypePassword")]
[DataType(DataType.Password)]
#endregion
public string ReTypePassword { get; set; }
}
这是我的界面:
public interface IPersonViewModel
{
string MobileNumber { get; set; }
string Password { get; set; }
string ReTypePassword { get; set; }
}
和人员类:
public abstract class Person
{
#region MainProperties
public Guid PersonId { get; set; }
#region DataValidatorModel
[Required(ErrorMessage = "ErrorInsertYourFirstName")]
[Display(Name = "FirstName")]
[StringLength(15, MinimumLength = 2, ErrorMessage = "ErrorMin2Max15Words")]
[RegularExpression(pattern: @"(^[^-\s\d*]\D*$)", ErrorMessage = "ErrorNotAllowWhitspaceInFirstAndNumbers")]
#endregion
#region EFValidation
[Column(TypeName = "varchar(max)")]
#endregion
public string FirstName { get; set; }
#region DataValidatorModel
[Required(ErrorMessage = "ErrorInsertYourLastName")]
[Display(Name = "LastName")]
[StringLength(15, MinimumLength = 2, ErrorMessage = "ErrorMin2Max15Words")]
[RegularExpression(pattern: @"(^[^-\s\d*]\D*$)", ErrorMessage = "ErrorNotAllowWhitspaceInFirstAndNumbers")]
#endregion
#region EFValidation
[Column(TypeName = "varchar(max)")]
#endregion
public string LastName { get; set; }
#region DataValidatorModel
[Required]
[Display(Name = "Gender")]
#endregion
#region EFValidation
[Column(TypeName = "varchar(max)")]
#endregion
public string Gender { get; set; }
#region DataValidatorModel
[Required(ErrorMessage = "ErrorInsertYourProfileImage")]
[Display(Name = "ProfileImage")]
//--RegularExpression ========================(test.jpg)||(folder/test.jpg)
[RegularExpression(pattern: @"([^\s]+(\.(?i)(jpg|png|gif|bmp))$)", ErrorMessage = "ErrorItsNotImageFile")]
#endregion
#region EFValidation
[Column(TypeName = "varchar(max)")]
#endregion
public string ProfileImage { get; set; }
#endregion
public string FullName => $"{FirstName} {LastName}";
}
这是我的儿童班:
public class UserViewModel:Person,IPersonViewModel
{
#region MainProperties
#region DataValidatorModel
[Display(Name ="Email")]
[DataType(DataType.EmailAddress)]
[RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$",ErrorMessage ="ErrorValidEmail")]
#endregion
public string Email { get; set; }
public string MobileNumber { get; set; }
public string Password { get; set; }
public string ReTypePassword { get; set; }
#endregion
}
我的问题是如何通过接口将数据注释继承给子类?
我不能在没有接口的情况下直接这样做,因为它显然是多重继承。
这是一个好方法吗?
有没有其他方法?
事实上,我不想对数据注释有多个引用。有没有办法做到这一点?