public static T CastOrFail <T>([CanBeNull][NoEnumeration] this object value, [CanBeNull] string name = null) { Type castType = typeof(T); Fail.IfNull(value, Violation.WhenCannotCast <T>(name ?? "object", value)); if (castType.IsEnum) { Fail.IfEnumNotDefined <T>(value); return((T)Enum.ToObject(castType, value)); } Fail.IfNotCastable <T>(value, Violation.WhenCannotCast <T>(name ?? "object", value)); return((T)value); }
public static void IfNullOrNotCastable <T>([CanBeNull][NoEnumeration] object value) { Fail.IfNull(value, Violation.WhenCannotCast <T>("object", value)); Fail.IfNotCastable <T>(value, Violation.WhenCannotCast <T>("object", value)); }
public static T AsOrFail <T>([CanBeNull][NoEnumeration] this object value, [CanBeNull] string name = null) { Fail.IfNotCastable <T>(value, Violation.WhenCannotCast <T>(name ?? "object", value)); return((T)value); }