/// <summary> /// Execute the switch statement. Optionally ensure that one case statement was satisfied. /// </summary> private static void Do(object value, bool guaranteeHandling, SwitchCase[] cases) { #region Input Validation Insist.IsNotNull(cases, "cases"); Insist.AllItemsAreNotNull(cases, "cases"); Insist.ContainsAtLeast(cases, 1, "cases"); EnsureDefaultIsNotTheOnlyCase(cases); EnsureSingleDefaultCase(cases); EnsureDefaultIsLastCase(cases); EnsureFallThroughCaseIsNotTheLastCase(cases); #endregion CopyActionsToFallThroughCases(cases); bool handled = false; foreach (SwitchCase c in cases) { if (c.IsDefaultCase || object.Equals(value, c.ValueToTest)) { c.ActionToPerform(); handled = true; break; } } if (guaranteeHandling == true && handled == false) { throw new InvalidOperationException("No cases in this switch statement have been satisfied."); } }
/// <summary> /// Construct a new Failure OperationResult with the supplied list of error messages /// </summary> /// <param name="errorMessages"> /// The collection of error messages that indicate why the operation failed /// </param> /// <exception cref="System.ArgumentNullException"> /// Throw if /// errorMessages is null /// errorMessages contains a null element /// </exception> /// <exception cref="System.ArgumentException"> /// Throw if /// errorMessages contains no elements /// </exception> public OperationResult(ICollection <string> errorMessages) { #region Input Validation Insist.IsNotNull(errorMessages, "errorMessages"); Insist.ContainsAtLeast(errorMessages, 1, "errorMessages"); Insist.AllItemsAreNotNull(errorMessages, "errorMessages"); #endregion _success = false; _errorMessages = new List <string>(); _errorMessages.AddRange(errorMessages); }
/// <summary> /// Ensures that the collection contains at least the number of specified items /// </summary> /// <param name="collection"> /// The collection to be validated. /// </param> /// <param name="minNumberOfItems"> /// The minimum number of items that the collection must have /// </param> /// <param name="argumentName"> /// The argument name. /// </param> /// <exception cref="System.ArgumentNullException"> /// If the collection itself is null /// </exception> /// <exception cref="System.ArgumentException"> /// If the collection contains less than the minumum number of items. OR. the minNumberOfItems /// argument is less than zero. /// </exception> public static void ContainsAtLeast <T>(IEnumerable <T> collection, int minNumberOfItems, string argumentName) { Insist.ContainsAtLeast(collection, minNumberOfItems, argumentName, null); }