public static T Exception <T>(string message, params object[] args) where T : Exception { throw Switch.Type <T, Exception>(exactType: true) .When <ArgumentException>(() => BuildArgumentException(message, args)) .Otherwise(() => BuildExceptionFallback <T>(message, args)); }
public static IEnumerable <T> Sort <T>( [NotNull] this IEnumerable <T> items, [NotNull, InstantHandle] Func <T, T, bool> predicate ) { Require.NotNull(items, "items"); Require.NotNull(predicate, "predicate"); Comparison <T> comparison = (left, right) => predicate(left, right) ? 1 : -1; return(Switch.Type <IEnumerable <T>, IEnumerable <T> >(items) .When <T[]>(array => { Array.Sort(array, comparison); return array; }) .When <List <T> >(list => { list.Sort(comparison); return list; }) .Otherwise(enumerable => { var list = enumerable.ToList(); list.Sort(comparison); return list; })); }
public static void NotEmpty <T>(IEnumerable items, string message, params object[] args) where T : Exception { Require.NotNull <T>(items, message, args); var hasItems = Switch.Type <IEnumerable, bool>(items) .When <ICollection>(collection => collection.Count > 0) .Otherwise(enumerable => enumerable.Cast <object>().Any()); if (!hasItems) { Require.Exception <T>(message, args); } }