internal static ArgumentException BodyShouldBeOfType(this LambdaExpression expression, string paramName, params Type[] allowedTypes) { switch (allowedTypes.Length) { case 0: throw new ArgumentException("At least one type needs to be allowed.", nameof(allowedTypes)); case 1: return(expression.BodyShouldBeOfType(paramName, allowedTypes[0])); default: var sb = new StringBuilder(); foreach (var type in allowedTypes.Take(allowedTypes.Length - 2)) { sb.Append(type.Name).Append(", "); } sb.Append(allowedTypes[allowedTypes.Length - 2]).Append(" or ") .Append(allowedTypes[allowedTypes.Length - 1]); return(expression.BodyShouldBeOfTypeImpl(paramName, sb.ToString())); } }
/// <summary> /// Return the body of the given lambda expression as the requested /// expression type. /// </summary> /// <typeparam name="T">The expected body expression type.</typeparam> /// <param name="expression">Lambda expression to extract the body from.</param> /// <returns>The body of the input expression.</returns> /// /// <exception cref="ArgumentException"> /// The body of <paramref name="expression"/> is not an instance of <typeparamref name="T"/>. /// </exception> public static T ExtractBody <T>(this LambdaExpression expression) where T : Expression => expression.Body as T ?? throw expression.BodyShouldBeOfType(nameof(expression), typeof(T));