bool IsNumeric(TypeCode tc)
{
switch (tc)
{
case TypeCode.SByte:
case TypeCode.Byte:
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
case TypeCode.UInt32:
case TypeCode.Int64:
case TypeCode.UInt64:
case TypeCode.Single:
case TypeCode.Double:
case TypeCode.Decimal:
return true;
default:
return false;
}
}
bool IsNumeric(Type type)
{
TypeCode typeCode = default;
if (IsNullAble(type))
{
var underlyingType = Nullable.GetUnderlyingType(type);
typeCode = Type.GetTypeCode(underlyingType);
return IsNumeric(typeCode);
}
typeCode = Type.GetTypeCode(type);
return IsNumeric(typeCode);
}
bool IsNullAble(Type type)
{
return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
}
public static string GetTypeAlias(Type type)
{
using (var provider = new CSharpCodeProvider())
{
var typeRef = new CodeTypeReference(type);
var typeAlias = provider.GetTypeOutput(typeRef);
if (typeAlias.Contains("System.Nullable<"))
{
return typeAlias.Replace(">", "?");
}
return typeAlias;
}
}