mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-12 11:15:12 +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:
@@ -45,6 +45,18 @@ namespace ICD.Common.Utils.Extensions
|
||||
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_TypeBaseTypes;
|
||||
private static readonly Dictionary<Type, Type[]> s_TypeImmediateInterfaces;
|
||||
@@ -61,6 +73,19 @@ namespace ICD.Common.Utils.Extensions
|
||||
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>
|
||||
/// Returns true if the given type is a numeric type.
|
||||
/// </summary>
|
||||
@@ -100,6 +125,19 @@ namespace ICD.Common.Utils.Extensions
|
||||
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>
|
||||
/// Gets the Assembly containing the type.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user