在我 class 中的 C# 属性上
On my C# properties in my class
我有这个class
我想验证 EmpType 属性 以便只能从枚举 EmploymentType 中获取值。
然后我想验证 EmploymentNumber,使其以 "E" 开头并包含 3 个数字
然后工资属性必须大于等于0.
public class Employment
{
[Required, StringLength(50, MinimumLength = 3, ErrorMessage ="Name must be between 3 and 50 characters long.")]
public string Name { get; set; }
public EmploymentType EmpType { get; set; }
public string EmplyomentNumber { get; set; }
public decimal Salary { get; set; }
}
public enum EmploymentType
{
Full,
Temporary,
Internship
}
您不能使用自动实现的属性创建验证。您应该创建一个带有字段的 属性:
public class Employment
{
[Required, StringLength(50, MinimumLength = 3, ErrorMessage ="Name must be between 3 and 50 characters long.")]
public string Name { get; set; }
private string _emplyomentNumber;
private decimal _salary;
public EmploymentType EmpType { get; set; }
public string EmplyomentNumber
{
get { return _emplyomentNumber; }
set
{
// validate here!
_emplyomentNumber = value;
}
}
public decimal Salary
{
get { return _salary; }
set
{
// validate here!
_salary = value;
}
}
}
public enum EmploymentType
{
Full,
Temporary,
Internship
}
您不需要对 EmploymentType
进行验证,除非某些枚举值无效。
要验证枚举,您应该这样做:
public EmploymentType EmpType
{
get
{
return empType;
}
set
{
if (EmploymentType.IsDefined(typeof(EmploymentType), value))
{
empType = value;
}
else
{
throw new InvalidEnumArgumentException("Error");
}
}
}
private EmploymentType empType;
如果要验证 EmployeeNumber
,请使用 RegularExpressionAttribute
。例如:
[RegularExpression("^[Ee]\d{3}$")]
public string EmployeeNumber{ get; set; }
至于EmploymentType
,这里的问题是如何让用户select这个值?是通过下拉列表、单选按钮等方式实现的吗?如果您使用枚举中的值填充下拉列表,我认为您只需要用 [Required]
属性装饰 EmpType
属性。
对于 Salary
属性 你可以使用 [Range(0.1, 100000.0)]
。第一个值代表最小允许值,后者是最大允许值。这两个值都是必需的。您不能只指定最小值而不指定最大值,反之亦然。
有关可用数据注释的更多信息,请查看 MSDN Documentation。
我有这个class
我想验证 EmpType 属性 以便只能从枚举 EmploymentType 中获取值。
然后我想验证 EmploymentNumber,使其以 "E" 开头并包含 3 个数字
然后工资属性必须大于等于0.
public class Employment { [Required, StringLength(50, MinimumLength = 3, ErrorMessage ="Name must be between 3 and 50 characters long.")] public string Name { get; set; } public EmploymentType EmpType { get; set; } public string EmplyomentNumber { get; set; } public decimal Salary { get; set; } } public enum EmploymentType { Full, Temporary, Internship }
您不能使用自动实现的属性创建验证。您应该创建一个带有字段的 属性:
public class Employment
{
[Required, StringLength(50, MinimumLength = 3, ErrorMessage ="Name must be between 3 and 50 characters long.")]
public string Name { get; set; }
private string _emplyomentNumber;
private decimal _salary;
public EmploymentType EmpType { get; set; }
public string EmplyomentNumber
{
get { return _emplyomentNumber; }
set
{
// validate here!
_emplyomentNumber = value;
}
}
public decimal Salary
{
get { return _salary; }
set
{
// validate here!
_salary = value;
}
}
}
public enum EmploymentType
{
Full,
Temporary,
Internship
}
您不需要对 EmploymentType
进行验证,除非某些枚举值无效。
要验证枚举,您应该这样做:
public EmploymentType EmpType
{
get
{
return empType;
}
set
{
if (EmploymentType.IsDefined(typeof(EmploymentType), value))
{
empType = value;
}
else
{
throw new InvalidEnumArgumentException("Error");
}
}
}
private EmploymentType empType;
如果要验证 EmployeeNumber
,请使用 RegularExpressionAttribute
。例如:
[RegularExpression("^[Ee]\d{3}$")]
public string EmployeeNumber{ get; set; }
至于EmploymentType
,这里的问题是如何让用户select这个值?是通过下拉列表、单选按钮等方式实现的吗?如果您使用枚举中的值填充下拉列表,我认为您只需要用 [Required]
属性装饰 EmpType
属性。
对于 Salary
属性 你可以使用 [Range(0.1, 100000.0)]
。第一个值代表最小允许值,后者是最大允许值。这两个值都是必需的。您不能只指定最小值而不指定最大值,反之亦然。
有关可用数据注释的更多信息,请查看 MSDN Documentation。