diff --git a/ICD.Common.Utils.Tests/Extensions/TypeExtensionsTest.cs b/ICD.Common.Utils.Tests/Extensions/TypeExtensionsTest.cs index 1f8fb59..f6450bb 100644 --- a/ICD.Common.Utils.Tests/Extensions/TypeExtensionsTest.cs +++ b/ICD.Common.Utils.Tests/Extensions/TypeExtensionsTest.cs @@ -10,6 +10,14 @@ namespace ICD.Common.Utils.Tests.Extensions [TestFixture] public sealed class TypeExtensionsTest { + [TestCase(typeof(byte), false)] + [TestCase(typeof(byte?), true)] + [TestCase(typeof(string), true)] + public void CanBeNullTest(Type value, bool expected) + { + Assert.AreEqual(expected, value.CanBeNull()); + } + [TestCase(typeof(byte), true)] [TestCase(typeof(decimal), true)] [TestCase(typeof(double), true)] @@ -61,6 +69,23 @@ namespace ICD.Common.Utils.Tests.Extensions Assert.AreEqual(expected, value.IsDecimalNumeric()); } + [TestCase(typeof(byte), true)] + [TestCase(typeof(decimal), false)] + [TestCase(typeof(double), false)] + [TestCase(typeof(float), false)] + [TestCase(typeof(int), true)] + [TestCase(typeof(long), true)] + [TestCase(typeof(sbyte), true)] + [TestCase(typeof(short), true)] + [TestCase(typeof(uint), true)] + [TestCase(typeof(ulong), true)] + [TestCase(typeof(ushort), true)] + [TestCase(typeof(string), false)] + public void IsIntegerNumericTest(Type value, bool expected) + { + Assert.AreEqual(expected, value.IsIntegerNumeric()); + } + [TestCase(typeof(string), typeof(object), true)] [TestCase(typeof(object), typeof(string), false)] public void IsAssignableToTest(Type a, Type b, bool expected) diff --git a/ICD.Common.Utils/Extensions/TypeExtensions.cs b/ICD.Common.Utils/Extensions/TypeExtensions.cs index ae79c64..34829aa 100644 --- a/ICD.Common.Utils/Extensions/TypeExtensions.cs +++ b/ICD.Common.Utils/Extensions/TypeExtensions.cs @@ -45,6 +45,18 @@ namespace ICD.Common.Utils.Extensions typeof(float), }; + private static readonly IcdHashSet s_IntegerNumericTypes = new IcdHashSet + { + typeof(byte), + typeof(int), + typeof(long), + typeof(sbyte), + typeof(short), + typeof(uint), + typeof(ulong), + typeof(ushort) + }; + private static readonly Dictionary s_TypeAllTypes; private static readonly Dictionary s_TypeBaseTypes; private static readonly Dictionary s_TypeImmediateInterfaces; @@ -61,6 +73,19 @@ namespace ICD.Common.Utils.Extensions s_TypeMinimalInterfaces = new Dictionary(); } + /// + /// Returns true if the given type can represent a null value. + /// + /// + /// + public static bool CanBeNull(this Type extends) + { + if (extends == null) + throw new ArgumentException("extends"); + + return !extends.IsValueType || Nullable.GetUnderlyingType(extends) != null; + } + /// /// Returns true if the given type is a numeric type. /// @@ -100,6 +125,19 @@ namespace ICD.Common.Utils.Extensions return s_DecimalNumericTypes.Contains(extends); } + /// + /// Returns true if the given type is an integer numeric type. + /// + /// + /// + public static bool IsIntegerNumeric(this Type extends) + { + if (extends == null) + throw new ArgumentException("extends"); + + return s_IntegerNumericTypes.Contains(extends); + } + /// /// Gets the Assembly containing the type. ///