From 07c9cc43d4bcae518555926f4c9cf70647519e08 Mon Sep 17 00:00:00 2001 From: DomCR Date: Mon, 29 Apr 2024 11:47:55 +0200 Subject: [PATCH] ThrowIf --- .../Extensions/StringExtensionsTests.cs | 14 +++++++++ CSUtilities/Extensions/ObjectExtensions.cs | 20 +++++++++++++ CSUtilities/Extensions/StringExtensions.cs | 29 +++++++++++++++++-- 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/CSUtilities.Tests/Extensions/StringExtensionsTests.cs b/CSUtilities.Tests/Extensions/StringExtensionsTests.cs index deb95d0..8c45d52 100644 --- a/CSUtilities.Tests/Extensions/StringExtensionsTests.cs +++ b/CSUtilities.Tests/Extensions/StringExtensionsTests.cs @@ -29,4 +29,18 @@ public void TrowIfNullOrEmptyTest(string value) Assert.Throws(() => value.TrowIfNullOrEmpty("Message in case of null or empty")); } } + + public class ObjectExtensionsTests + { + [Fact] + public void ThrowIfTest() + { + int zero = 0; + + zero.ThrowIf((value) => + { + return value == 0; + }); + } + } } diff --git a/CSUtilities/Extensions/ObjectExtensions.cs b/CSUtilities/Extensions/ObjectExtensions.cs index a88fbba..4b32fd2 100644 --- a/CSUtilities/Extensions/ObjectExtensions.cs +++ b/CSUtilities/Extensions/ObjectExtensions.cs @@ -4,6 +4,8 @@ namespace CSUtilities.Extensions { internal static class ObjectExtensions { + public delegate bool Check(T obj); + public static void ThrowIfNull(this object parameter) { if (parameter != null) @@ -27,5 +29,23 @@ public static void ThrowIfNull(this object parameter, string paramName, string m throw new System.ArgumentNullException(paramName, message); } + + public static void ThrowIf(this T parameter, Check check) + where E : Exception, new() + { + if (check(parameter)) + { + throw new E(); + } + } + + public static void ThrowIf(this T parameter, Check check, string message) + where E : Exception, new() + { + if (check(parameter)) + { + throw Activator.CreateInstance(typeof(E), message) as E; + } + } } } diff --git a/CSUtilities/Extensions/StringExtensions.cs b/CSUtilities/Extensions/StringExtensions.cs index 8c23371..dd5accf 100644 --- a/CSUtilities/Extensions/StringExtensions.cs +++ b/CSUtilities/Extensions/StringExtensions.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Runtime.CompilerServices; namespace CSUtilities.Extensions { @@ -10,27 +9,53 @@ namespace CSUtilities.Extensions /// internal static class StringExtensions { + /// + /// Indicates if the specified string is null + /// + /// The string to test. + /// public static bool IsNull(this string str) { return str == null; } + /// + /// Indicates whether the specified string is null or an empty string (""). + /// + /// The string to test. + /// true if the value parameter is null or an empty string (""); otherwise, false. public static bool IsNullOrEmpty(this string str) { return string.IsNullOrEmpty(str); } + /// + /// Indicates whether a specified string is null, empty, or consists only of white-space characters. + /// + /// + /// true if the value parameter is null or System.String.Empty, or if value consists exclusively of white-space characters. public static bool IsNullOrWhiteSpace(this string str) { return string.IsNullOrWhiteSpace(str); } + /// + /// + /// + /// + /// public static void TrowIfNullOrEmpty(this string str) { str.TrowIfNullOrEmpty("String cannot be null or empty"); } - public static void TrowIfNullOrEmpty(this string str, string message = null) + /// + /// + /// + /// + /// + /// + public static void TrowIfNullOrEmpty(this string str, string message) { if (string.IsNullOrEmpty(str)) {