コード例 #1
0
ファイル: ObjectInspector.cs プロジェクト: zuohd/Composable
 /// <summary>
 /// <para>Throws <see cref="ObjectIsNullContractViolationException"/> if any inspected value is null</para>
 /// <para>Consider using <see cref="NotNullOrDefault{TArgument}"/> instead as it works for value types as well and is only marginally slower.</para>
 /// </summary>
 public static Inspected <TValue> NotNull <TValue>(this Inspected <TValue> me)
     where TValue : class
 {
     return(me.Inspect(
                inspected => !ReferenceEquals(inspected, null),
                badValue => new ObjectIsNullContractViolationException(badValue)));
 }
コード例 #2
0
 ///<summary>
 /// <para>Throws <see cref="ObjectIsNullContractViolationException"/> if any expected value is null.</para>
 /// <para>Throws <see cref="StringIsEmptyContractViolationException"/> if any inspected value is an empty string.</para>
 /// <para>Throws <see cref="StringIsWhitespaceContractViolationException"/> if any inspected value is a string containing only whitespace.</para>
 /// </summary>
 public static Inspected <String> NotNullEmptyOrWhiteSpace(this Inspected <String> me)
 {
     me.NotNullOrEmpty(); //We want the proper exceptions
     return(me.Inspect(
                inspected => inspected.Trim() != String.Empty,
                badValue => new StringIsWhitespaceContractViolationException(badValue)));
 }
コード例 #3
0
        ///<summary>
        /// <para>Throws <see cref="ObjectIsNullContractViolationException"/> if any inspected value is null.</para>
        /// <para>Throws a <see cref="EnumerableIsEmptyContractViolationException"/> if any inspected value is an empty sequence.</para>
        /// </summary>
        public static Inspected <TValue> NotNullOrEmptyEnumerable <TValue>(this Inspected <TValue> me)
            where TValue : IEnumerable
        {
            me.Inspect(
                inspected => !ReferenceEquals(inspected, null),
                badValue => new ObjectIsNullContractViolationException(badValue));

            return(me.Inspect(
                       inspected => inspected.Cast <object>().Any(),
                       badValue => new EnumerableIsEmptyContractViolationException(badValue)));
        }
コード例 #4
0
ファイル: ObjectInspector.cs プロジェクト: zuohd/Composable
        /// <summary>
        /// <para>Throws <see cref="ObjectIsNullContractViolationException"/> if any inspected value is null</para>
        /// <para>Throws <see cref="ObjectIsDefaultContractViolationException"/> if any inspected value is default(TValue). Such as 0 for integer, Guid.Empty for Guid, new SomeStruct().</para>
        /// </summary>
        public static Inspected <TValue> NotNullOrDefault <TValue>(this Inspected <TValue> me)
        {
            me.Inspect(
                inspected => !ReferenceEquals(inspected, null),
                badValue => new ObjectIsNullContractViolationException(badValue));

            return(me.Inspect(
                       inspected =>
            {
                if (!inspected.GetType().IsValueType)
                {
                    return true;
                }
                var valueTypeDefault = Activator.CreateInstance(inspected.GetType());
                return !Equals(inspected, valueTypeDefault);
            },
                       badValue => new ObjectIsDefaultContractViolationException(badValue)));
        }
コード例 #5
0
 ///<summary>Throws a <see cref="GuidIsEmptyContractViolationException"/> if any inspected value is Guid.Empty</summary>
 public static Inspected <Guid> NotEmpty(this Inspected <Guid> me)
 {
     return(me.Inspect(
                inspected => inspected != Guid.Empty,
                badValue => new GuidIsEmptyContractViolationException(badValue)));
 }
コード例 #6
0
 ///<summary>
 /// <para>Throws <see cref="ObjectIsNullContractViolationException"/> if any expected value is null.</para>
 /// <para>Throws <see cref="StringIsEmptyContractViolationException"/> if any inspected value is an empty string.</para>
 /// </summary>
 public static Inspected <string> NotNullOrEmpty(this Inspected <string> me)
 {
     me.NotNull(); //We want the proper exceptions
     return(me.Inspect(inspected => inspected != String.Empty,
                       badValue => new StringIsEmptyContractViolationException(badValue)));
 }