예제 #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);
        }
        public static T CastOrFail <T>([CanBeNull][NoEnumeration] this object value, [CanBeNull] string name = null)
        {
            Type castedType = typeof(T);

            Fail.IfNull(value, Fail.notCastableMessageWithName, name ?? "object", castedType, "null");

            if (castedType.IsEnum)
            {
                Fail.IfEnumNotDefined <T>(value);
                return((T)Enum.ToObject(castedType, value));
            }

            Fail.IfNotCastable <T>(value, Fail.notCastableMessageWithName, name ?? "object", castedType, value);
            return((T)value);
        }