From d9c8d3e19a0f7d3ec94a4374e43acd0df6f54e6d Mon Sep 17 00:00:00 2001 From: ig Date: Thu, 14 Sep 2023 14:24:06 +0200 Subject: [PATCH] Split NullableUtils.cs into Class and Struct submodules --- csharp/Lib/Utils/NullableUtils.cs | 17 ++---------- csharp/Lib/Utils/NullableUtilsClass.Class.cs | 26 +++++++++++++++++++ .../Lib/Utils/NullableUtilsStruct.Struct.cs | 24 +++++++++++++++++ csharp/Lib/Utils/Utils.cs | 7 ++--- 4 files changed, 54 insertions(+), 20 deletions(-) create mode 100644 csharp/Lib/Utils/NullableUtilsClass.Class.cs create mode 100644 csharp/Lib/Utils/NullableUtilsStruct.Struct.cs diff --git a/csharp/Lib/Utils/NullableUtils.cs b/csharp/Lib/Utils/NullableUtils.cs index 315aa1adb..58a6c2f16 100644 --- a/csharp/Lib/Utils/NullableUtils.cs +++ b/csharp/Lib/Utils/NullableUtils.cs @@ -2,20 +2,7 @@ namespace InnovEnergy.Lib.Utils; public static class NullableUtils { - public static T? Nullable(this T t) where T : struct => t; + // ReSharper disable once ReturnTypeCanBeNotNullable + public static T? AsNullable(this T t) => t; - public static IEnumerable Enumerable(this T? t) where T : struct - { - if (t.HasValue) - yield return t.Value; - } - - - public static T ThrowIfNull(this T? t, String message) where T:struct - { - if (!t.HasValue) - throw new NullReferenceException(message); - - return t.Value; - } } \ No newline at end of file diff --git a/csharp/Lib/Utils/NullableUtilsClass.Class.cs b/csharp/Lib/Utils/NullableUtilsClass.Class.cs new file mode 100644 index 000000000..4bd01b34c --- /dev/null +++ b/csharp/Lib/Utils/NullableUtilsClass.Class.cs @@ -0,0 +1,26 @@ +using System.Diagnostics.CodeAnalysis; + +namespace InnovEnergy.Lib.Utils; + +public static class NullableUtilsClass +{ + public static IEnumerable ToEnumerable(this T? t) where T : class + { + if (t is not null) + yield return t; + } + + public static T ThrowIfNull(this T? t) where T : class => ThrowIfNull(t, null); + + public static T ThrowIfNull(this T? t, String? message) where T : class + { + if (t is null) + throw new NullReferenceException(message); + + return t; + } + + public static Boolean IsNull([NotNullWhen(returnValue: false)] this T? t) where T : class => t is null; + + public static T? As(this Object t) where T : class => t as T; +} \ No newline at end of file diff --git a/csharp/Lib/Utils/NullableUtilsStruct.Struct.cs b/csharp/Lib/Utils/NullableUtilsStruct.Struct.cs new file mode 100644 index 000000000..7bee56222 --- /dev/null +++ b/csharp/Lib/Utils/NullableUtilsStruct.Struct.cs @@ -0,0 +1,24 @@ +using System.Diagnostics.CodeAnalysis; + +namespace InnovEnergy.Lib.Utils; + +public static class NullableUtilsStruct +{ + public static IEnumerable ToEnumerable(this T? t) where T : struct + { + if (t.HasValue) + yield return t.Value; + } + + public static T ThrowIfNull(this T? t) where T : struct => ThrowIfNull(t, null); + + public static T ThrowIfNull(this T? t, String? message) where T : struct + { + if (!t.HasValue) + throw new NullReferenceException(message); + + return t.Value; + } + + public static Boolean IsNull([NotNullWhen(returnValue: false)] this T? t) where T : struct => t.HasValue; +} \ No newline at end of file diff --git a/csharp/Lib/Utils/Utils.cs b/csharp/Lib/Utils/Utils.cs index 40b7be82f..7f7d441c5 100644 --- a/csharp/Lib/Utils/Utils.cs +++ b/csharp/Lib/Utils/Utils.cs @@ -6,9 +6,10 @@ using static System.Runtime.CompilerServices.MethodImplOptions; namespace InnovEnergy.Lib.Utils; + public static class Utils { - public static Boolean IsNull([NotNullWhen(returnValue: false)] this T t) => t is null; + public static IEnumerable GetEnumStrings(this T e) where T : Enum { @@ -169,9 +170,5 @@ public static class Utils CopyFilesRecursively(dir, target.CreateSubdirectory(dir.Name)); } - public static String ExecutingProcessName => Process.GetCurrentProcess().ProcessName; - - - } \ No newline at end of file