📄 commonrules.cs
字号:
if (e.Description == string.Empty)
{
e.Description = string.Format("{0} exceed the maximum number of words", e.FriendlyName, text1);
}
return false;
}
return true;
}
#endregion
#region RegexIsMatch
/// <summary>
/// Rule ensuring a String value is matching
/// a specified regular expression.
/// </summary>
/// <param name="target">Object containing the data to validate.</param>
/// <param name="e"><see cref="ValidationRuleArgs"/> containing the information about the object to be validated, must be of type RegexRuleArgs</param>
/// <returns>False if the rule is broken; true otherwise.</returns>
/// <remarks>
/// This implementation uses late binding, and will only work
/// against String property values.
/// </remarks>
public static bool RegexIsMatch(object target, ValidationRuleArgs e)
{
RegexRuleArgs args = e as RegexRuleArgs;
if (args != null)
{
string expression = args.Expression;
PropertyInfo p = target.GetType().GetProperty(e.PropertyName);
if (p != null)
{
if (p.PropertyType == typeof(string))
{
string value = (string)p.GetValue(target, null);
if (value == null || !Regex.IsMatch(value, expression))
{
if (string.IsNullOrEmpty(e.Description)) e.Description = String.Format("{0} is not valid.", e.FriendlyName);
return false;
}
return true;
}
else
{
throw new ArgumentException(string.Format("Property \"{0}\" is not of type String.", e.PropertyName));
}
}
else
{
throw new ArgumentException(string.Format("Property \"{0}\" not found on object \"{1}\"", e.PropertyName, target.GetType().ToString()));
}
}
else
{
throw new ArgumentException("Invalid ValidationRuleArgs. e must be of type RegexRuleArgs.");
}
}
/// <summary>
/// Class used with the <see cref="RegexIsMatch"/>.
/// </summary>
public class RegexRuleArgs : ValidationRuleArgs
{
private string _expression;
/// <summary>
/// The Regular expression that the string have to match.
/// </summary>
public string Expression
{
get { return _expression; }
}
/// <summary>
/// Initializes a new instance of the RegexRuleArgs class.
/// </summary>
/// <param name="propertyName">Property to validate</param>
/// <param name="friendlyName">Friendly name to use in the validation error text.</param>
/// <param name="expression">The Regular expression that the property have to match</param>
public RegexRuleArgs(string propertyName, string friendlyName, string expression)
: base(propertyName, friendlyName)
{
_expression = expression;
}
/// <summary>
/// Initializes a new instance of the RegexRuleArgs class.
/// </summary>
/// <param name="propertyName">Property to validate</param>
/// <param name="expression">The Regular expression that the property have to match</param>
public RegexRuleArgs(string propertyName, string expression)
: base(propertyName)
{
_expression = expression;
}
/// <summary>
/// Return a string representation of the object.
/// </summary>
public override string ToString()
{
return base.ToString() + "!" + _expression;
}
}
#endregion
#region CompareValues
/// <summary>
/// Generic rule that determines if an object's property is less than a particular value.
/// </summary>
/// <typeparam name="T">Datatype of the property to validate</typeparam>
/// <param name="target">Object containing the data to validate.</param>
/// <param name="e"><see cref="ValidationRuleArgs"/> containing the information about the object to be validated.</param>
/// <returns>False if the rule is broken; true otherwise.</returns>
public static bool LessThanValue<T>(object target, ValidationRuleArgs e)
{
return CompareValues<T>(target, e as CompareValueRuleArgs<T>, CompareType.LessThan);
}
/// <summary>
/// Generic rule that determines if an object's property is less than or equal to a particular value.
/// </summary>
/// <typeparam name="T">Datatype of the property to validate</typeparam>
/// <param name="target">Object containing the data to validate.</param>
/// <param name="e"><see cref="ValidationRuleArgs"/> containing the information about the object to be validated.</param>
/// <returns>False if the rule is broken; true otherwise.</returns>
public static bool LessThanOrEqualToValue<T>(object target, ValidationRuleArgs e)
{
return CompareValues<T>(target, e as CompareValueRuleArgs<T>, CompareType.LessThanOrEqualTo);
}
/// <summary>
/// Generic rule that determines if an object's property is equal to a particular value.
/// </summary>
/// <typeparam name="T">Datatype of the property to validate</typeparam>
/// <param name="target">Object containing the data to validate.</param>
/// <param name="e"><see cref="ValidationRuleArgs"/> containing the information about the object to be validated.</param>
/// <returns>False if the rule is broken; true otherwise.</returns>
public static bool EqualsValue<T>(object target, ValidationRuleArgs e)
{
return CompareValues<T>(target, e as CompareValueRuleArgs<T>, CompareType.EqualTo);
}
/// <summary>
/// Generic rule that determines if an object's property is greater than a particular value.
/// </summary>
/// <typeparam name="T">Datatype of the property to validate</typeparam>
/// <param name="target">Object containing the data to validate.</param>
/// <param name="e"><see cref="ValidationRuleArgs"/> containing the information about the object to be validated.</param>
/// <returns>False if the rule is broken; true otherwise.</returns>
public static bool GreaterThanValue<T>(object target, ValidationRuleArgs e)
{
return CompareValues<T>(target, e as CompareValueRuleArgs<T>, CompareType.GreaterThan
);
}
/// <summary>
/// Generic rule that determines if an object's property is greater than or equal to a particular value.
/// </summary>
/// <typeparam name="T">Datatype of the property to validate</typeparam>
/// <param name="target">Object containing the data to validate.</param>
/// <param name="e"><see cref="ValidationRuleArgs"/> containing the information about the object to be validated.</param>
/// <returns>False if the rule is broken; true otherwise.</returns>
public static bool GreaterThanOrEqualToValue<T>(object target, ValidationRuleArgs e)
{
return CompareValues<T>(target, e as CompareValueRuleArgs<T>, CompareType.GreaterThanOrEqualTo);
}
/// <summary>
/// Private method that compares a property value with a specified value.
/// </summary>
/// <typeparam name="T">Datatype of the property to validate.</typeparam>
/// <param name="target">Object containing the data to validate.</param>
/// <param name="e"><see cref="ValidationRuleArgs"/> containing the information about the object to be validated.</param>
/// <param name="compareType"><see cref="CompareType"/> defining the type of comparison that will be made.</param>
/// <returns></returns>
private static bool CompareValues<T>(object target, CompareValueRuleArgs<T> e, CompareType compareType)
{
bool result = true;
if (e != null)
{
T compareValue = e.CompareValue;
PropertyInfo p = target.GetType().GetProperty(e.PropertyName);
T value;
//if (p.PropertyType.Name.Equals(typeof(Nullable<>).Name))
//{
//}
try
{
value = (T)p.GetValue(target, null);
}
catch (Exception)
{
return true;
}
// if the property is read from a nullable type, then a null valid is considered as allowed
if (p.PropertyType.Name.Equals(typeof(Nullable<>).Name) && value == null)
{
return true;
}
int res = Comparer.DefaultInvariant.Compare(value, compareValue);
switch (compareType)
{
case CompareType.LessThanOrEqualTo:
result = (res <= 0);
if (!result)
{
if (string.IsNullOrEmpty(e.Description))
{
e.Description = string.Format("{0} cannot exceed {1}.", e.FriendlyName, compareValue.ToString());
}
}
break;
case CompareType.LessThan:
result = (res < 0);
if (!result)
{
if (string.IsNullOrEmpty(e.Description))
{
e.Description = string.Format("{0} must be less than {1}.", e.FriendlyName, compareValue.ToString());
}
}
break;
case CompareType.EqualTo:
result = (res == 0);
if (!result)
{
if (string.IsNullOrEmpty(e.Description))
{
e.Description = string.Format("{0} must equal {1}.", e.FriendlyName, compareValue.ToString());
}
}
break;
case CompareType.GreaterThan:
result = (res > 0);
if (!result)
{
if (string.IsNullOrEmpty(e.Description))
{
e.Description = string.Format("{0} must exceed {1}.", e.FriendlyName, compareValue.ToString());
}
}
break;
case CompareType.GreaterThanOrEqualTo:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -