示例#1
0
        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);
        }
示例#2
0
 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));
 }
示例#3
0
        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);
        }