private static bool CheckRange <T>(object inValue, PossibleValueRangeAttribute possibleValue) where T : IComparable <T> { try { T maxValue = (T)GetValueFromString(typeof(T), possibleValue.MaxValue); T minValue = (T)GetValueFromString(typeof(T), possibleValue.MinValue); T checkingValue = (T)inValue; if (maxValue.CompareTo(minValue) < 0) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "max value: {0} could not be less than min value: {1}", maxValue.ToString(), minValue.ToString())); } if ((checkingValue.CompareTo(maxValue) <= 0 && checkingValue.CompareTo(minValue) >= 0)) { return(true); } } catch (FormatException ex) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Format exception happens when convert {0} or {1} to {2}, information: {3}", possibleValue.MinValue, possibleValue.MaxValue, inValue.GetType().Name, ex.ToString())); } catch (OverflowException ex) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "{0} or {1} overflowed as {2}, information: {3}", possibleValue.MinValue, possibleValue.MaxValue, inValue.GetType().Name, ex.ToString())); } catch (Exception ex) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Error happens when check value range, information: {0}", ex.ToString())); } return(false); }
/// <summary> /// Check if the given value is within the possible value range. /// </summary> /// <param name="inValue">The given value</param> /// <param name="possibleValueAttribute">The possible value range</param> /// <returns>True for yes; False for no.</returns> public static bool CheckValueByRange(object inValue, PossibleValueRangeAttribute possibleValueAttribute) { String typeName = inValue.GetType().Name; bool checkResult; switch (typeName) { case "Char": checkResult = CheckRange <Char>(inValue, possibleValueAttribute); break; case "Byte": checkResult = CheckRange <Byte>(inValue, possibleValueAttribute); break; case "SByte": checkResult = CheckRange <SByte>(inValue, possibleValueAttribute); break; case "Int16": checkResult = CheckRange <Int16>(inValue, possibleValueAttribute); break; case "UInt16": checkResult = CheckRange <UInt16>(inValue, possibleValueAttribute); break; case "Int32": checkResult = CheckRange <Int32>(inValue, possibleValueAttribute); break; case "UInt32": checkResult = CheckRange <UInt32>(inValue, possibleValueAttribute); break; case "Int64": checkResult = CheckRange <Int64>(inValue, possibleValueAttribute); break; case "UInt64": checkResult = CheckRange <UInt64>(inValue, possibleValueAttribute); break; case "Double": checkResult = CheckRange <Double>(inValue, possibleValueAttribute); break; case "Single": checkResult = CheckRange <Single>(inValue, possibleValueAttribute); break; case "Decimal": checkResult = CheckRange <Decimal>(inValue, possibleValueAttribute); break; default: throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "{0} data type is not supported", typeName)); } return(checkResult); }