public static void AssertGenericArgumentNotNull <T>(T arg, string argName) { Type type = typeof(T); if (type.IsValueType && (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(Nullable <>))) { return; } ArgumentHelper.AssertNotNull <object>((object)arg, argName); }
/// <summary> /// Attempts to convert the specified value back. /// </summary> /// <param name="value"> /// The value to convert. /// </param> /// <param name="targetType"> /// The type of the binding target property. /// </param> /// <param name="parameter"> /// The converter parameter to use. /// </param> /// <param name="culture"> /// The culture to use in the converter. /// </param> /// <returns> /// A converted value. /// </returns> public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { ArgumentHelper.AssertNotNull(targetType, "targetType"); try { return(System.Convert.ChangeType(value, targetType, culture)); } catch (Exception) { return(DependencyProperty.UnsetValue); } }
public static void AssertNotNull <T>(IEnumerable <T> arg, string argName, bool assertContentsNotNull) { ArgumentHelper.AssertNotNull <IEnumerable <T> >(arg, argName); if (!assertContentsNotNull || !typeof(T).IsClass) { return; } foreach (T obj in arg) { if ((object)obj == null) { throw new ArgumentException("An item inside the enumeration was null.", argName); } } }
public static void AssertNotNull <T>(this IEnumerable <T> arg, string argName, bool assertContentsNotNull) { ArgumentHelper.AssertNotNull <T>(arg, argName, assertContentsNotNull); }
public static void AssertNotNull <T>(this T?arg, string argName) where T : struct { ArgumentHelper.AssertNotNull <T>(arg, argName); }
public static void AssertEnumMember <TEnum>(TEnum enumValue, string argName, params TEnum[] validValues) where TEnum : struct, IConvertible { ArgumentHelper.AssertNotNull <TEnum[]>(validValues, "validValues"); if (Attribute.IsDefined((MemberInfo)typeof(TEnum), typeof(FlagsAttribute), false)) { long num = enumValue.ToInt64((IFormatProvider)CultureInfo.InvariantCulture); bool flag; if (num == 0L) { flag = true; foreach (TEnum @enum in validValues) { if (@enum.ToInt64((IFormatProvider)CultureInfo.InvariantCulture) == 0L) { flag = false; break; } } } else { foreach (TEnum @enum in validValues) { num &= [email protected]((IFormatProvider)CultureInfo.InvariantCulture); } flag = num != 0L; } if (!flag) { return; } throw new ArgumentException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "Enum value '{0}' is not allowed for flags enumeration '{1}'.", new object[2] { (object)enumValue, (object)typeof(TEnum).FullName }), argName); } else { foreach (TEnum @enum in validValues) { if (enumValue.Equals((object)@enum)) { return; } } if (!Enum.IsDefined(typeof(TEnum), (object)enumValue)) { throw new ArgumentException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "Enum value '{0}' is not defined for enumeration '{1}'.", new object[2] { (object)enumValue, (object)typeof(TEnum).FullName }), argName); } else { throw new ArgumentException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "Enum value '{0}' is defined for enumeration '{1}' but it is not permitted in this context.", new object[2] { (object)enumValue, (object)typeof(TEnum).FullName }), argName); } } }