using System; using System.Collections.Generic; using System.ComponentModel; using System.Globalization; using System.Linq; using System.Runtime.InteropServices; namespace ArchestrAServices.Common; public static class ThrowHelper { [AttributeUsage(AttributeTargets.Parameter)] private sealed class ValidatedNotNullAttribute : Attribute { } public static void ThrowIfArgumentNull([ValidatedNotNull] T argument, string argumentName) where T : class { if (argument == null) { throw new ArgumentNullException(argumentName); } } public static void ThrowIfArgumentNullOrEmpty([ValidatedNotNull] IEnumerable argument, string argumentName) { if (argument == null) { throw new ArgumentNullException(argumentName); } if (!argument.Any()) { throw new ArgumentException("Value must not be empty.", argumentName); } } public static void ThrowIfArgumentNullOrWhiteSpace([ValidatedNotNull] string argument, string argumentName) { if (argument == null) { throw new ArgumentNullException(argumentName); } if (string.IsNullOrWhiteSpace(argument)) { throw new ArgumentException("Value must not be empty or only whitespace.", argumentName); } } public static void ThrowIfArgumentOutOfRange(bool outOfRange, string argumentName) { if (outOfRange) { throw new ArgumentOutOfRangeException(argumentName, "Value out of range."); } } public static void ThrowArgumentException(bool invalid, string argumentName, [Localizable(false)] string message) { if (invalid) { throw new ArgumentException(message, argumentName); } } public static void ThrowInvalidOperation([Localizable(false)] string message) { throw new InvalidOperationException(message); } public static void ThrowIfServiceIsNull([ValidatedNotNull] T service) where T : class { if (service == null) { throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Service {0} is not available", new object[1] { typeof(T).FullName })); } } public static void ThrowIfHResultError(int code) { Marshal.ThrowExceptionForHR(code); } }