/// <summary> /// 抛出验证失败的异常 /// </summary> /// <param name="validateBase">特性验证基类</param> /// <param name="po">属性项</param> private void ThrowValidateException(PropertyValidateAttribute validateBase, ChangedProperty changedProperty) { Check.IfNullOrZero(changedProperty); Check.IfNullOrZero(validateBase); var reason = validateBase.FailReason($@"{changedProperty.DeclaringType}.{changedProperty.PropertyName}"); throw new Exception(reason); }
/// <summary> /// 对属性设置默认值 /// </summary> /// <param name="defaultValueAttribute">默认值</param> /// <param name="propertyItem">属性项</param> /// <param name="rawPropertyValue">原始的属性值</param> private void SetPropertyDefaultValue(DefaultValueAttribute defaultValueAttribute, ChangedProperty propertyItem, Object rawPropertyValue) { Check.IfNullOrZero(defaultValueAttribute); Check.IfNullOrZero(propertyItem); var propertyInstanceValue = rawPropertyValue; var propertyInstanceValueType = propertyItem.Type; var isDefaultValue = propertyInstanceValue.ToString() == (propertyInstanceValueType.IsValueType ? Activator.CreateInstance(propertyInstanceValueType).ToString() : null); //判断是否为字符串类型的属性值为空 if (propertyInstanceValueType == typeof(String) && String.IsNullOrEmpty(propertyInstanceValue + "")) { propertyItem.Value = defaultValueAttribute.Value; } else if ((propertyInstanceValueType.IsValueType && propertyInstanceValueType.IsNumeric()) && isDefaultValue) //判断是否为值类型并且值为值类型的默认值 { propertyItem.Value = defaultValueAttribute.Value; } else if (propertyInstanceValue.GetType() == typeof(DateTime) && isDefaultValue) //判断是否为时间类型并且时间类型的值为默认值 { propertyItem.Value = defaultValueAttribute.Value; } else if (propertyInstanceValue.GetType() == typeof(Boolean) && isDefaultValue) { propertyItem.Value = defaultValueAttribute.Value; } }