mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 13:15:07 +00:00
feat: Extension methods for determining if a type represents an integer number, or if a type can represent a null value
This commit is contained in:
@@ -10,6 +10,14 @@ namespace ICD.Common.Utils.Tests.Extensions
|
|||||||
[TestFixture]
|
[TestFixture]
|
||||||
public sealed class TypeExtensionsTest
|
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(byte), true)]
|
||||||
[TestCase(typeof(decimal), true)]
|
[TestCase(typeof(decimal), true)]
|
||||||
[TestCase(typeof(double), true)]
|
[TestCase(typeof(double), true)]
|
||||||
@@ -61,6 +69,23 @@ namespace ICD.Common.Utils.Tests.Extensions
|
|||||||
Assert.AreEqual(expected, value.IsDecimalNumeric());
|
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(string), typeof(object), true)]
|
||||||
[TestCase(typeof(object), typeof(string), false)]
|
[TestCase(typeof(object), typeof(string), false)]
|
||||||
public void IsAssignableToTest(Type a, Type b, bool expected)
|
public void IsAssignableToTest(Type a, Type b, bool expected)
|
||||||
|
|||||||
@@ -45,6 +45,18 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
typeof(float),
|
typeof(float),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private static readonly IcdHashSet<Type> s_IntegerNumericTypes = new IcdHashSet<Type>
|
||||||
|
{
|
||||||
|
typeof(byte),
|
||||||
|
typeof(int),
|
||||||
|
typeof(long),
|
||||||
|
typeof(sbyte),
|
||||||
|
typeof(short),
|
||||||
|
typeof(uint),
|
||||||
|
typeof(ulong),
|
||||||
|
typeof(ushort)
|
||||||
|
};
|
||||||
|
|
||||||
private static readonly Dictionary<Type, Type[]> s_TypeAllTypes;
|
private static readonly Dictionary<Type, Type[]> s_TypeAllTypes;
|
||||||
private static readonly Dictionary<Type, Type[]> s_TypeBaseTypes;
|
private static readonly Dictionary<Type, Type[]> s_TypeBaseTypes;
|
||||||
private static readonly Dictionary<Type, Type[]> s_TypeImmediateInterfaces;
|
private static readonly Dictionary<Type, Type[]> s_TypeImmediateInterfaces;
|
||||||
@@ -61,6 +73,19 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
s_TypeMinimalInterfaces = new Dictionary<Type, Type[]>();
|
s_TypeMinimalInterfaces = new Dictionary<Type, Type[]>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns true if the given type can represent a null value.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="extends"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool CanBeNull(this Type extends)
|
||||||
|
{
|
||||||
|
if (extends == null)
|
||||||
|
throw new ArgumentException("extends");
|
||||||
|
|
||||||
|
return !extends.IsValueType || Nullable.GetUnderlyingType(extends) != null;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns true if the given type is a numeric type.
|
/// Returns true if the given type is a numeric type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -100,6 +125,19 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
return s_DecimalNumericTypes.Contains(extends);
|
return s_DecimalNumericTypes.Contains(extends);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns true if the given type is an integer numeric type.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="extends"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool IsIntegerNumeric(this Type extends)
|
||||||
|
{
|
||||||
|
if (extends == null)
|
||||||
|
throw new ArgumentException("extends");
|
||||||
|
|
||||||
|
return s_IntegerNumericTypes.Contains(extends);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the Assembly containing the type.
|
/// Gets the Assembly containing the type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user