/// <summary> /// Throws an <paramref name="exception"/> when the <paramref name="affectedMethod" /> is called. /// </summary> /// <param name="affectedMethod">The method to affect.</param> /// <param name="exception">The <see cref="Exception"/> that should be thrown.</param> /// <exception cref="ArgumentNullException">Thrown when <paramref name="exception"/> is a null reference.</exception> public static Affector Throw(this IAffectedMethod affectedMethod, Exception exception) { if (exception == null) { throw new ArgumentNullException(nameof(exception)); } return((Affector)affectedMethod.AddAffector(new ExceptionThrower(exception))); }
// Affectors /// <summary> /// Slows the <paramref name="affectedMethod"/> down by the given <paramref name="time"/>. /// </summary> /// <param name="affectedMethod">The method to affect.</param> /// <param name="time">A <see cref="TimeSpan"/> value indicating the amount of time used to slow down the call.</param> /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="time"/> equals zero or a negative amount of time.</exception> public static Affector SlowItDownBy(this IAffectedMethod affectedMethod, TimeSpan time) { if (time.Ticks <= 0) { // TODO Needs explanation throw new ArgumentOutOfRangeException(); // we can't speed up things } return((Affector)affectedMethod.AddAffector(new Delayer(time))); }
/// <summary> /// Throws an exception of type <paramref name="exceptionType"/> when the <paramref name="affectedMethod" /> is called. /// </summary> /// <param name="affectedMethod">The method to affect.</param> /// <param name="exceptionType">A <see cref="Type"/> of <see cref="Exception"/> that should be thrown.</param> /// <exception cref="ArgumentNullException">Thrown when <paramref name="exceptionType"/> is a null reference.</exception> /// <exception cref="ArgumentException">Thrown when <paramref name="exceptionType"/> is not referring to a reference type based on <see cref="Exception"/>.</exception> public static Affector Throw(this IAffectedMethod affectedMethod, Type exceptionType) { if (exceptionType == null) { throw new ArgumentNullException(nameof(exceptionType)); } var ti = exceptionType.GetTypeInfo(); if (!ti.IsAssignableFrom(typeof(Exception).GetTypeInfo()) && !ti.IsSubclassOf(typeof(Exception))) { //TODO move to resources file throw new ArgumentException("The given exception type is not System.Exception nor derived from System.Exception", nameof(exceptionType)); } var ex = Activator.CreateInstance(exceptionType) as Exception; return(Throw(affectedMethod, ex)); }
/// <summary> /// Throws an exception of type <typeparamref name="TException"/> when the <paramref name="affectedMethod" /> is called. /// </summary> /// <typeparam name="TException">A reference type derived from <see cref="Exception"/>.</typeparam> /// <param name="affectedMethod">The method to affect.</param> /// <exception cref="ArgumentException">Thrown when <typeparamref name="TException"/> is not referring to a reference type based on <see cref="Exception"/>.</exception> public static Affector Throw <TException>(this IAffectedMethod affectedMethod) where TException : Exception { return(Throw(affectedMethod, typeof(TException))); }