public static TValue GetOrException <TKey, TValue> (this IDictionary <TKey, TValue> @this, TKey key, Type exceptionType = null) { TValue value; if (@this.TryGetValue(key, out value)) { return(value); } else { throw ExceptionExts.Create(exceptionType ?? typeof(KeyNotFoundException), ErrorKeyNotFound.Fmt(key)); } }
public static TValue GetOrException <TKey, TValue, TException> (this IDictionary <TKey, TValue> @this, TKey key) where TException : Exception { TValue value; if (@this.TryGetValue(key, out value)) { return(value); } else { throw ExceptionExts.Create <TException>(ErrorKeyNotFound.Fmt(key)); } }
public static T FirstOrException <T> (this IEnumerable <T> @this, Func <T, bool> predicate, Type exceptionType = null) { Contract.Requires <ArgumentNullException>(@this != null, "this"); Contract.Requires <ArgumentNullException>(predicate != null, "predicate"); Contract.Requires <ArgumentException>(exceptionType == null || exceptionType.Is <Exception>(), "exceptionType"); foreach (T item in @this) { if (predicate(item)) { return(item); } } throw ExceptionExts.Create(exceptionType ?? typeof(InvalidOperationException), "Sequence of '{0}' contains no matching element.".Fmt(typeof(T).GetMediumName())); }