Split NullableUtils.cs into Class and Struct submodules

This commit is contained in:
ig 2023-09-14 14:24:06 +02:00
parent 8d4eadc540
commit d9c8d3e19a
4 changed files with 54 additions and 20 deletions

View File

@ -2,20 +2,7 @@ namespace InnovEnergy.Lib.Utils;
public static class NullableUtils
{
public static T? Nullable<T>(this T t) where T : struct => t;
// ReSharper disable once ReturnTypeCanBeNotNullable
public static T? AsNullable<T>(this T t) => t;
public static IEnumerable<T> Enumerable<T>(this T? t) where T : struct
{
if (t.HasValue)
yield return t.Value;
}
public static T ThrowIfNull<T>(this T? t, String message) where T:struct
{
if (!t.HasValue)
throw new NullReferenceException(message);
return t.Value;
}
}

View File

@ -0,0 +1,26 @@
using System.Diagnostics.CodeAnalysis;
namespace InnovEnergy.Lib.Utils;
public static class NullableUtilsClass
{
public static IEnumerable<T> ToEnumerable<T>(this T? t) where T : class
{
if (t is not null)
yield return t;
}
public static T ThrowIfNull<T>(this T? t) where T : class => ThrowIfNull(t, null);
public static T ThrowIfNull<T>(this T? t, String? message) where T : class
{
if (t is null)
throw new NullReferenceException(message);
return t;
}
public static Boolean IsNull<T>([NotNullWhen(returnValue: false)] this T? t) where T : class => t is null;
public static T? As<T>(this Object t) where T : class => t as T;
}

View File

@ -0,0 +1,24 @@
using System.Diagnostics.CodeAnalysis;
namespace InnovEnergy.Lib.Utils;
public static class NullableUtilsStruct
{
public static IEnumerable<T> ToEnumerable<T>(this T? t) where T : struct
{
if (t.HasValue)
yield return t.Value;
}
public static T ThrowIfNull<T>(this T? t) where T : struct => ThrowIfNull(t, null);
public static T ThrowIfNull<T>(this T? t, String? message) where T : struct
{
if (!t.HasValue)
throw new NullReferenceException(message);
return t.Value;
}
public static Boolean IsNull<T>([NotNullWhen(returnValue: false)] this T? t) where T : struct => t.HasValue;
}

View File

@ -6,9 +6,10 @@ using static System.Runtime.CompilerServices.MethodImplOptions;
namespace InnovEnergy.Lib.Utils;
public static class Utils
{
public static Boolean IsNull<T>([NotNullWhen(returnValue: false)] this T t) => t is null;
public static IEnumerable<String> GetEnumStrings<T>(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;
}