Tuesday, August 23, 2016

Check If Type of Primitive for .NET

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public static bool IsSimpleType(Type itemType)
{
var t = itemType;

if (t.IsGenericType && t.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) { t = Nullable.GetUnderlyingType(t); }

switch (Type.GetTypeCode(t))
{
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
case TypeCode.SByte:
case TypeCode.Single:
case TypeCode.String:
case TypeCode.DateTime:
case TypeCode.Decimal:
case TypeCode.Boolean:
case TypeCode.Byte:
case TypeCode.Char:
case TypeCode.Double:
return true;
}
return false;
}


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <Extension()> _
Public Function IsSimpleType(ItemType As Type) As Boolean
Dim t = ItemType

If t.IsGenericType AndAlso t.GetGenericTypeDefinition().Equals(GetType(Nullable(Of ))) Then
t = Nullable.GetUnderlyingType(t)
End If

Select Case Type.GetTypeCode(t)
Case TypeCode.Int16, TypeCode.Int32, TypeCode.Int64, TypeCode.UInt16, TypeCode.UInt32, TypeCode.UInt64, _
TypeCode.[SByte], TypeCode.[Single], TypeCode.[String], TypeCode.DateTime, TypeCode.[Decimal], TypeCode.[Boolean], _
TypeCode.[Byte], TypeCode.[Char], TypeCode.[Double]
Return True
End Select
Return False
End Function

No comments:

Post a Comment