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:
Chris Cameron
2018-04-05 11:26:11 -04:00
parent 3239307f66
commit 007663a193
2 changed files with 63 additions and 0 deletions

View File

@@ -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>