From 7dd4201971f1bb19ea380f2ca83aed11206cfe97 Mon Sep 17 00:00:00 2001 From: AmbulantRex <21176662+AmbulantRex@users.noreply.github.com> Date: Sun, 9 Apr 2023 10:53:09 -0600 Subject: Reconcile pre-packaged meta.json against manifest on install --- src/Jellyfin.Extensions/TypeExtensions.cs | 45 +++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/Jellyfin.Extensions/TypeExtensions.cs (limited to 'src') diff --git a/src/Jellyfin.Extensions/TypeExtensions.cs b/src/Jellyfin.Extensions/TypeExtensions.cs new file mode 100644 index 000000000..5b1111d59 --- /dev/null +++ b/src/Jellyfin.Extensions/TypeExtensions.cs @@ -0,0 +1,45 @@ +using System; +using System.Globalization; + +namespace Jellyfin.Extensions; + +/// +/// Provides extensions methods for . +/// +public static class TypeExtensions +{ + /// + /// Checks if the supplied value is the default or null value for that type. + /// + /// The type of the value to compare. + /// The type. + /// The value to check. + /// if the value is the default for the type. Otherwise, . + public static bool IsNullOrDefault(this Type type, T value) + { + if (value is null) + { + return true; + } + + object? tmp = value; + object? defaultValue = type.IsValueType ? Activator.CreateInstance(type) : null; + if (type.IsAssignableTo(typeof(IConvertible))) + { + tmp = Convert.ChangeType(value, type, CultureInfo.InvariantCulture); + } + + return Equals(tmp, defaultValue); + } + + /// + /// Checks if the object is currently a default or null value. Boxed types will be unboxed prior to comparison. + /// + /// The object to check. + /// if the value is the default for the type. Otherwise, . + public static bool IsNullOrDefault(this object? obj) + { + // Unbox the type and check. + return obj?.GetType().IsNullOrDefault(obj) ?? true; + } +} -- cgit v1.2.3