- 特性信息类
/// <summary>
/// 为元素添加验证信息的特性类
/// </summary>
[AttributeUsage(AttributeTargets.All)]
public class ValidateAttribute : Attribute
{
/// <summary>
/// 验证类型
/// </summary>
private ValidateType _validateType;
/// <summary>
/// 最小长度
/// </summary>
private int _minLength;
/// <summary>
/// 最大长度
/// </summary>
private int _maxLength;
/// <summary>
/// 自定义数据源
/// </summary>
private string[] _customArray;
/// <summary>
/// 验证类型
/// </summary>
public ValidateType ValidateType
{
get { return this._validateType; }
}
/// <summary>
/// 最小长度
/// </summary>
public int MinLength
{
get { return this._minLength; }
set { this._minLength = value; }
}
/// <summary>
/// 最大长度
/// </summary>
public int MaxLength
{
get { return this._maxLength; }
set { this._maxLength = value; }
}
/// <summary>
/// 自定义数据源
/// </summary>
public string[] CustomArray
{
get { return this._customArray; }
set { this._customArray = value; }
}
/// <summary>
/// 指定采取何种验证方式来验证元素的有效性
/// </summary>
/// <param name="validateType"></param>
public ValidateAttribute(ValidateType validateType)
{
this._validateType = validateType;
}
}
/// <summary>
/// 验证类型
/// 验证非空 [Validate(ValidateType.IsEmpty)]
/// 验证非空和最大长度 [Validate(ValidateType.IsEmpty | ValidateType.MaxLength, MaxLength = 50)]
/// 验证非空和数值 [Validate(ValidateType.IsEmpty | ValidateType.IsNumber)]
/// 验证非空和decimal [Validate(ValidateType.IsEmpty | ValidateType.IsDecimal)]
/// 验证非空和时间 [Validate(ValidateType.IsEmpty | ValidateType.IsDateTime)]
/// 验证非空和数据源 [Validate(ValidateType.IsEmpty | ValidateType.IsInCustomArray, CustomArray = new string[] { "现结", "到付", "月结" })]
///
/// 如果需要新增验证类型,请在LD.Util/AttributeCheck/GetValidateResult中新增switch内容即可,记住不要忘记新增ValidateType内容
/// </summary>
[Flags]
public enum ValidateType
{
/// <summary>
/// 字段或属性是否为空字串
/// </summary>
IsEmpty = 0x0001,
/// <summary>
/// 字段或属性的最小长度
/// </summary>
MinLength = 0x0002,
/// <summary>
/// 字段或属性的最大长度
/// </summary>
MaxLength = 0x0004,
/// <summary>
/// 字段或属性的值是否为数值型
/// </summary>
IsNumber = 0x0008,
/// <summary>
/// 字段或属性的值是否为时间类型
/// </summary>
IsDateTime = 0x0010,
/// <summary>
/// 字段或属性的值是否为正确的浮点类型
/// </summary>
IsDecimal = 0x0020,
/// <summary>
/// 字段或属性的值是否包含在指定的数据源数组中
/// </summary>
IsInCustomArray = 0x0040,
/// <summary>
/// 字段或属性的值是否为固定电话号码格式
/// </summary>
IsTelphone = 0x0080,
/// <summary>
/// 字段或属性的值是否为手机号码格式
/// </summary>
IsMobile = 0x0100
}
- 验证实体类特性的方法
/// <summary>
/// 特性的校验
/// </summary>
public class AttributeCheck
{
/// <summary>
/// 验证属性值是否满足特性需求
/// </summary>
/// <param name="entityObject"></param>
/// <param name="isAppend">提示内容是否允许拼接</param>
/// <returns>返回检验提示内容</returns>
public static string GetValidateResult(object entityObject,bool isAppend)
{
try
{
if (entityObject == null) return "特性验证中传入的参数为空!";
Type type = entityObject.GetType();
PropertyInfo[] properties = type.GetProperties();
string validateResult = string.Empty;
foreach (PropertyInfo property in properties)
{
//获取验证特性
object[] validateContent = property.GetCustomAttributes(typeof(ValidateAttribute), true);
if (validateContent != null)
{
//获取属性的值
object value = property.GetValue(entityObject, null);
foreach (ValidateAttribute validateAttribute in validateContent)
{
switch (validateAttribute.ValidateType)
{
//验证元素是否为空字串
case ValidateType.IsEmpty:
if (null == value || value.ToString().Length < 1)
validateResult += string.Format("元素 {0} 不能为空 ", property.Name);
break;
//验证元素的长度是否小于指定最小长度
case ValidateType.MinLength:
if (null == value || value.ToString().Length < 1) break;
if (value.ToString().Length < validateAttribute.MinLength)
validateResult += string.Format("元素 {0} 的长度不能小于 {1} ", property.Name, validateAttribute.MinLength);
break;
//验证元素的长度是否大于指定最大长度
case ValidateType.MaxLength:
if (null == value || value.ToString().Length < 1) break;
if (value.ToString().Length > validateAttribute.MaxLength)
validateResult += string.Format("元素 {0} 的长度不能大于{1} ", property.Name, validateAttribute.MaxLength);
break;
//验证元素的长度是否符合指定的最大长度和最小长度的范围
case ValidateType.MinLength | ValidateType.MaxLength:
if (null == value || value.ToString().Length < 1) break;
if (value.ToString().Length > validateAttribute.MaxLength || value.ToString().Length < validateAttribute.MinLength)
validateResult += string.Format("元素 {0} 不符合指定的最小长度和最大长度的范围,应该在 {1} 与 {2} 之间", property.Name, validateAttribute.MinLength, validateAttribute.MaxLength);
break;
//验证元素的值是否为值类型
case ValidateType.IsNumber:
if (null == value || value.ToString().Length < 1) break;
if (!System.Text.RegularExpressions.Regex.IsMatch(value.ToString(), @"^\d+$"))
validateResult += string.Format("元素 {0} 的值不是值类型 ", property.Name);
break;
//验证元素的值是否为正确的时间格式
case ValidateType.IsDateTime:
if (null == value || value.ToString().Length < 1) break;
if (!System.Text.RegularExpressions.Regex.IsMatch(value.ToString(), @"(\d{2,4})[-/]?([0]?[1-9]|[1][12])[-/]?([0][1-9]|[12]\d|[3][01])\s*([01]\d|[2][0-4])?[:]?([012345]?\d)?[:]?([012345]?\d)?"))
validateResult += string.Format("元素 {0} 不是正确的时间格式 ", property.Name);
break;
//验证元素的值是否为正确的浮点格式
case ValidateType.IsDecimal:
if (null == value || value.ToString().Length < 1) break;
if (!System.Text.RegularExpressions.Regex.IsMatch(value.ToString(), @"^\d+[.]?\d+$"))
validateResult += string.Format("元素 {0} 不是正确的金额格式 ", property.Name);
break;
//验证元素的值是否在指定的数据源中
case ValidateType.IsInCustomArray:
if (null == value || value.ToString().Length < 1) break;
if (null == validateAttribute.CustomArray || validateAttribute.CustomArray.Length < 1)
validateResult += string.Format("系统内部错误:元素 {0} 指定的数据源为空或没有数据 ", property.Name);
bool isHas = Array.Exists<string>(validateAttribute.CustomArray, delegate (string str)
{
return str == value.ToString();
}
);
if (!isHas)
validateResult += string.Format("元素 {0} 的值设定不正确 , 应该为 {1} 中的一种 ", property.Name, string.Join(",", validateAttribute.CustomArray));
break;
//验证元素的值是否为固定电话号码格式
case ValidateType.IsTelphone:
if (null == value || value.ToString().Length < 1) break;
if (!System.Text.RegularExpressions.Regex.IsMatch(value.ToString(), @"^(\d{3,4}-)?\d{6,8}$"))
validateResult += string.Format("元素 {0} 不是正确的固定电话号码格式 ", property.Name);
break;
//验证元素的值是否为手机号码格式
case ValidateType.IsMobile:
if (null == value || value.ToString().Length < 1) break;
if (!System.Text.RegularExpressions.Regex.IsMatch(value.ToString(), @"^[1]+[3,5]+\d{9}$"))
validateResult += string.Format("元素 {0} 不是正确的手机号码格式 ", property.Name);
break;
//验证元素是否为空且符合指定的最小长度
case ValidateType.IsEmpty | ValidateType.MinLength:
if (null == value || value.ToString().Length < 1) goto case ValidateType.IsEmpty;
goto case ValidateType.MinLength;
//验证元素是否为空且符合指定的最大长度
case ValidateType.IsEmpty | ValidateType.MaxLength:
if (null == value || value.ToString().Length < 1) goto case ValidateType.IsEmpty;
goto case ValidateType.MaxLength;
//验证元素是否为空且符合指定的长度范围
case ValidateType.IsEmpty | ValidateType.MinLength | ValidateType.MaxLength:
if (null == value || value.ToString().Length < 1) goto case ValidateType.IsEmpty;
goto case ValidateType.MinLength | ValidateType.MaxLength;
//验证元素是否为空且值为数值型
case ValidateType.IsEmpty | ValidateType.IsNumber:
if (null == value || value.ToString().Length < 1) goto case ValidateType.IsEmpty;
goto case ValidateType.IsNumber;
//验证元素是否为空且值为浮点型
case ValidateType.IsEmpty | ValidateType.IsDecimal:
if (null == value || value.ToString().Length < 1) goto case ValidateType.IsEmpty;
goto case ValidateType.IsDecimal;
//验证元素是否为空且值为时间类型
case ValidateType.IsEmpty | ValidateType.IsDateTime:
if (null == value || value.ToString().Length < 1) goto case ValidateType.IsEmpty;
goto case ValidateType.IsDateTime;
//验证元素是否为空且值在指定的数据源中
case ValidateType.IsEmpty | ValidateType.IsInCustomArray:
if (null == value || value.ToString().Length < 1) goto case ValidateType.IsEmpty;
goto case ValidateType.IsInCustomArray;
//验证元素是否为空且值为固定电话号码格式
case ValidateType.IsEmpty | ValidateType.IsTelphone:
if (null == value || value.ToString().Length < 1) goto case ValidateType.IsEmpty;
goto case ValidateType.IsTelphone;
//验证元素是否为空且值为手机号码格式
case ValidateType.IsEmpty | ValidateType.IsMobile:
if (null == value || value.ToString().Length < 1) goto case ValidateType.IsEmpty;
goto case ValidateType.IsMobile;
default:
break;
}
}
}
if (!isAppend)
if (!string.IsNullOrEmpty(validateResult)) break;
}
return validateResult;
}
catch (Exception ex)
{
return ex.ToString();
}
}
}
- 实体类上的特性标注
public class FQCHeader
{
/// <summary>
/// 唯一编号
/// 必需字段
/// 最大长度90
/// 供应商代号+序列号+创建时间(年月日,时分秒,毫秒3位)
/// </summary>
[Validate(ValidateType.IsEmpty | ValidateType.MaxLength, MaxLength = 90)]
public string pk_id { get; set; }
/// <summary>
/// 供应商编号
/// 必需字段
/// 最大长度10
/// </summary>
[Validate(ValidateType.IsEmpty | ValidateType.MaxLength, MaxLength = 10)]
public string lifnr { get; set; }
/// <summary>
/// 序列号
/// 必需字段
/// 最大长度18
/// </summary>
[Validate(ValidateType.IsEmpty | ValidateType.MaxLength, MaxLength = 18)]
public string barcd { get; set; }
/// <summary>
/// 工单号
/// 必需字段
/// 最大长度20
/// </summary>
[Validate(ValidateType.IsEmpty | ValidateType.MaxLength, MaxLength = 20)]
public string wknum { get; set; }
/// <summary>
/// 产线
/// 必需字段
/// 最大长度10
/// </summary>
[Validate(ValidateType.IsEmpty | ValidateType.MaxLength, MaxLength = 10)]
public string pline { get; set; }
/// <summary>
/// 工序编号
/// 必需字段
/// 最大长度5
/// </summary>
[Validate(ValidateType.IsEmpty | ValidateType.MaxLength, MaxLength = 5)]
public string processid { get; set; }
/// <summary>
/// 扫描工位
/// 最大长度6
/// </summary>
[Validate(ValidateType.MaxLength, MaxLength = 6)]
public string wkposition { get; set; }
/// <summary>
/// 检验批号
/// 最大长度12
/// </summary>
[Validate(ValidateType.MaxLength, MaxLength = 12)]
public string prueflos { get; set; }
/// <summary>
/// 检验结果
/// 必需字段
/// 最大长度2
/// 合格Q1;不合格Q2;记录不良Q3
/// </summary>
[Validate(ValidateType.IsEmpty | ValidateType.MaxLength, MaxLength = 2)]
public string result { get; set; }
/// <summary>
/// 是否批量
/// 必需字段
/// 最大长度10
/// 0非批量,1批量
/// </summary>
[Validate(ValidateType.IsEmpty | ValidateType.MaxLength, MaxLength = 10)]
public string is_batch { get; set; }
/// <summary>
/// 处理决策
/// 最大长度100
/// </summary>
[Validate(ValidateType.MaxLength, MaxLength = 100)]
public string deal_method { get; set; }
/// <summary>
/// 不良原因
/// 最大长度200
/// </summary>
[Validate(ValidateType.MaxLength, MaxLength = 200)]
public string reason { get; set; }
/// <summary>
/// 改善措施
/// 最大长度200
/// </summary>
[Validate(ValidateType.MaxLength, MaxLength = 200)]
public string inprove_method { get; set; }
/// <summary>
/// 创建人
/// 必需字段
/// 最大长度40
/// 中文名
/// </summary>
[Validate(ValidateType.IsEmpty | ValidateType.MaxLength, MaxLength = 40)]
public string crname { get; set; }
/// <summary>
/// 创建时间
/// 必需字段
/// 年月日时分秒
/// </summary>
[Validate(ValidateType.IsEmpty)]
public string crtime { get; set; }
/// <summary>
/// 修改人
/// 最大长度40
/// 中文名
/// </summary>
[Validate(ValidateType.MaxLength, MaxLength = 40)]
public string chname { get; set; }
/// <summary>
/// 修改时间
/// 年月日时分秒
/// </summary>
public string chtime { get; set; }
/// <summary>
/// 更新标记
/// 最大长度1
/// U代表更新,D代表删除,I代表新增
/// </summary>
[Validate(ValidateType.MaxLength, MaxLength = 1)]
public string update_flag { get; set; }
/// <summary>
/// 预留字段1
/// 最大长度200
/// </summary>
[Validate(ValidateType.MaxLength, MaxLength = 200)]
public string reserved1 { get; set; }
/// <summary>
/// 预留字段2
/// 最大长度200
/// </summary>
[Validate(ValidateType.MaxLength, MaxLength = 200)]
public string reserved2 { get; set; }
/// <summary>
/// 预留字段3
/// 最大长度200
/// </summary>
[Validate(ValidateType.MaxLength, MaxLength = 200)]
public string reserved3 { get; set; }
/// <summary>
/// 预留字段4
/// 最大长度200
/// </summary>
[Validate(ValidateType.MaxLength, MaxLength = 200)]
public string reserved4 { get; set; }
}
- 调用方法
string Exception = GetValidateResult(receive, true);
说明:
GetValidateResult方法参数中的receive为实体对象