コード例 #1
0
 public bool Validate()
 {
     if (ValidationRule == null)
     {
         return(true);
     }
     Error   = ValidationRule.Check(Value) ? string.Empty : ValidationRule.ValidationMessage;
     IsValid = Error.Length == 0;
     Debug.WriteLine("Valid after validation = " + IsValid);
     if (!IsValid)
     {
         Debug.WriteLine("Error message = " + Error);
     }
     return(IsValid);
 }
コード例 #2
0
 /// <summary>
 ///   Prevents exceptions of the specified type from being thrown from executions of the specified rule, instead passing them to a handler <see
 ///    cref="Action{TException}" /> .
 /// </summary>
 /// <typeparam name="TException"> The exception type to handle. </typeparam>
 /// <typeparam name="TTarget"> The type of the target. </typeparam>
 /// <param name="rule"> The rule. </param>
 /// <param name="getDetails"> A handler which will be called if an exception of <typeparamref name="TException" /> is thrown by the rule. </param>
 /// <returns> </returns>
 public static ValidationRule <TTarget> Handle <TException, TTarget>(
     this ValidationRule <TTarget> rule,
     Action <TException> getDetails = null)
     where TException : Exception =>
 Validate.That <TTarget>(t =>
 {
     try
     {
         return(rule.Check(t));
     }
     catch (TException ex)
     {
         getDetails?.Invoke(ex);
         return(false);
     }
 });