code_junkie

Using .Net, how can I determine if a type is a Numeric ValueType?

Nov 11th, 2011
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.18 KB | None | 0 0
  1. Dim desiredType as Type
  2. if IsNumeric(desiredType) then ...
  3.  
  4. if ((desiredType.IsArray))
  5. return 0;
  6.  
  7. switch (Type.GetTypeCode(desiredType))
  8. {
  9. case 3:
  10. case 6:
  11. case 7:
  12. case 9:
  13. case 11:
  14. case 13:
  15. case 14:
  16. case 15:
  17. return 1;
  18. }
  19. ;return 0;
  20.  
  21. TypeCode typeCode = Type.GetTypeCode(desiredType);
  22.  
  23. if (typeCode == TypeCode.Double || typeCode == TypeCode.Integer || ...)
  24. return true;
  25.  
  26. /// <summary>
  27. /// Determines if a type is numeric. Nullable numeric types are considered numeric.
  28. /// </summary>
  29. /// <remarks>
  30. /// Boolean is not considered numeric.
  31. /// </remarks>
  32. public static bool IsNumericType( Type type )
  33. {
  34. if ( type == null )
  35. {
  36. return false;
  37. }
  38.  
  39. switch ( Type.GetTypeCode( type ) )
  40. {
  41. case TypeCode.Byte:
  42. case TypeCode.Decimal:
  43. case TypeCode.Double:
  44. case TypeCode.Int16:
  45. case TypeCode.Int32:
  46. case TypeCode.Int64:
  47. case TypeCode.SByte:
  48. case TypeCode.Single:
  49. case TypeCode.UInt16:
  50. case TypeCode.UInt32:
  51. case TypeCode.UInt64:
  52. return true;
  53. case TypeCode.Object:
  54. if ( type.IsGenericType && type.GetGenericTypeDefinition() == typeof( Nullable<> ) )
  55. {
  56. return IsNumericType( Nullable.GetUnderlyingType( type ) );
  57. }
  58. return false;
  59. }
  60. return false;
  61.  
  62. }
  63.  
  64.  
  65.  
  66. /// <summary>
  67. /// Tests the IsNumericType method.
  68. /// </summary>
  69. [Fact]
  70. public static void IsNumericTypeTest()
  71. {
  72.  
  73. // non numeric types
  74. Assert.False( TypeHelper.IsNumericType( null ) );
  75. Assert.False( TypeHelper.IsNumericType( typeof( object ) ) );
  76. Assert.False( TypeHelper.IsNumericType( typeof( DBNull ) ) );
  77. Assert.False( TypeHelper.IsNumericType( typeof( bool ) ) );
  78. Assert.False( TypeHelper.IsNumericType( typeof( char ) ) );
  79. Assert.False( TypeHelper.IsNumericType( typeof( DateTime ) ) );
  80. Assert.False( TypeHelper.IsNumericType( typeof( string ) ) );
  81.  
  82. // arrays of numeric and non-numeric types
  83. Assert.False( TypeHelper.IsNumericType( typeof( object[] ) ) );
  84. Assert.False( TypeHelper.IsNumericType( typeof( DBNull[] ) ) );
  85. Assert.False( TypeHelper.IsNumericType( typeof( bool[] ) ) );
  86. Assert.False( TypeHelper.IsNumericType( typeof( char[] ) ) );
  87. Assert.False( TypeHelper.IsNumericType( typeof( DateTime[] ) ) );
  88. Assert.False( TypeHelper.IsNumericType( typeof( string[] ) ) );
  89. Assert.False( TypeHelper.IsNumericType( typeof( byte[] ) ) );
  90. Assert.False( TypeHelper.IsNumericType( typeof( decimal[] ) ) );
  91. Assert.False( TypeHelper.IsNumericType( typeof( double[] ) ) );
  92. Assert.False( TypeHelper.IsNumericType( typeof( Int16[] ) ) );
  93. Assert.False( TypeHelper.IsNumericType( typeof( Int32[] ) ) );
  94. Assert.False( TypeHelper.IsNumericType( typeof( Int64[] ) ) );
  95. Assert.False( TypeHelper.IsNumericType( typeof( SByte[] ) ) );
  96. Assert.False( TypeHelper.IsNumericType( typeof( Single[] ) ) );
  97. Assert.False( TypeHelper.IsNumericType( typeof( UInt16[] ) ) );
  98. Assert.False( TypeHelper.IsNumericType( typeof( UInt32[] ) ) );
  99. Assert.False( TypeHelper.IsNumericType( typeof( UInt64[] ) ) );
  100.  
  101. // numeric types
  102. Assert.True( TypeHelper.IsNumericType( typeof( byte ) ) );
  103. Assert.True( TypeHelper.IsNumericType( typeof( decimal ) ) );
  104. Assert.True( TypeHelper.IsNumericType( typeof( double ) ) );
  105. Assert.True( TypeHelper.IsNumericType( typeof( Int16 ) ) );
  106. Assert.True( TypeHelper.IsNumericType( typeof( Int32 ) ) );
  107. Assert.True( TypeHelper.IsNumericType( typeof( Int64 ) ) );
  108. Assert.True( TypeHelper.IsNumericType( typeof( SByte ) ) );
  109. Assert.True( TypeHelper.IsNumericType( typeof( Single ) ) );
  110. Assert.True( TypeHelper.IsNumericType( typeof( UInt16 ) ) );
  111. Assert.True( TypeHelper.IsNumericType( typeof( UInt32 ) ) );
  112. Assert.True( TypeHelper.IsNumericType( typeof( UInt64 ) ) );
  113.  
  114. // nullable non-numeric types
  115. Assert.False( TypeHelper.IsNumericType( typeof( bool? ) ) );
  116. Assert.False( TypeHelper.IsNumericType( typeof( char? ) ) );
  117. Assert.False( TypeHelper.IsNumericType( typeof( DateTime? ) ) );
  118.  
  119. // nullable numeric types
  120. Assert.True( TypeHelper.IsNumericType( typeof( byte? ) ) );
  121. Assert.True( TypeHelper.IsNumericType( typeof( decimal? ) ) );
  122. Assert.True( TypeHelper.IsNumericType( typeof( double? ) ) );
  123. Assert.True( TypeHelper.IsNumericType( typeof( Int16? ) ) );
  124. Assert.True( TypeHelper.IsNumericType( typeof( Int32? ) ) );
  125. Assert.True( TypeHelper.IsNumericType( typeof( Int64? ) ) );
  126. Assert.True( TypeHelper.IsNumericType( typeof( SByte? ) ) );
  127. Assert.True( TypeHelper.IsNumericType( typeof( Single? ) ) );
  128. Assert.True( TypeHelper.IsNumericType( typeof( UInt16? ) ) );
  129. Assert.True( TypeHelper.IsNumericType( typeof( UInt32? ) ) );
  130. Assert.True( TypeHelper.IsNumericType( typeof( UInt64? ) ) );
  131.  
  132. // testing with GetType because of handling with non-numerics. see:
  133. // http://msdn.microsoft.com/en-us/library/ms366789.aspx
  134.  
  135. // using gettype - non-numeric
  136. Assert.False( TypeHelper.IsNumericType( (new object()).GetType() ) );
  137. Assert.False( TypeHelper.IsNumericType( DBNull.Value.GetType() ) );
  138. Assert.False( TypeHelper.IsNumericType( true.GetType()) );
  139. Assert.False( TypeHelper.IsNumericType( 'a'.GetType() ) );
  140. Assert.False( TypeHelper.IsNumericType( ( new DateTime( 2009 , 1 , 1 ) ).GetType() ) );
  141. Assert.False( TypeHelper.IsNumericType( "".GetType() ) );
  142.  
  143. // using gettype - numeric types
  144. // ReSharper disable RedundantCast
  145. Assert.True( TypeHelper.IsNumericType( ( new byte() ).GetType() ) );
  146. Assert.True( TypeHelper.IsNumericType( ( 43.2m ).GetType() ) );
  147. Assert.True( TypeHelper.IsNumericType( ( 43.2d ).GetType() ) );
  148. Assert.True( TypeHelper.IsNumericType( ( ( Int16 )2 ).GetType() ) );
  149. Assert.True( TypeHelper.IsNumericType( ( ( Int32 )2 ).GetType() ) );
  150. Assert.True( TypeHelper.IsNumericType( ( ( Int64 )2 ).GetType() ) );
  151. Assert.True( TypeHelper.IsNumericType( ( ( SByte )2 ).GetType() ) );
  152. Assert.True( TypeHelper.IsNumericType( 2f.GetType() ) );
  153. Assert.True( TypeHelper.IsNumericType( ( ( UInt16 )2 ).GetType() ) );
  154. Assert.True( TypeHelper.IsNumericType( ( ( UInt32 )2 ).GetType() ) );
  155. Assert.True( TypeHelper.IsNumericType( ( ( UInt64 )2 ).GetType() ) );
  156. // ReSharper restore RedundantCast
  157.  
  158. // using gettype - nullable non-numeric types
  159. bool? nullableBool = true;
  160. Assert.False( TypeHelper.IsNumericType( nullableBool.GetType() ) );
  161. char? nullableChar = ' ';
  162. Assert.False( TypeHelper.IsNumericType( nullableChar.GetType() ) );
  163. DateTime? nullableDateTime = new DateTime( 2009 , 1 , 1 );
  164. Assert.False( TypeHelper.IsNumericType( nullableDateTime.GetType() ) );
  165.  
  166. // using getttype - nullable numeric types
  167. byte? nullableByte = 12;
  168. Assert.True( TypeHelper.IsNumericType( nullableByte.GetType()) );
  169. decimal? nullableDecimal = 12.2m;
  170. Assert.True( TypeHelper.IsNumericType( nullableDecimal.GetType() ) );
  171. double? nullableDouble = 12.32;
  172. Assert.True( TypeHelper.IsNumericType( nullableDouble.GetType() ) );
  173. Int16? nullableInt16 = 12;
  174. Assert.True( TypeHelper.IsNumericType( nullableInt16.GetType() ) );
  175. Int16? nullableInt32 = 12;
  176. Assert.True( TypeHelper.IsNumericType( nullableInt32.GetType() ) );
  177. Int16? nullableInt64 = 12;
  178. Assert.True( TypeHelper.IsNumericType( nullableInt64.GetType() ) );
  179. SByte? nullableSByte = 12;
  180. Assert.True( TypeHelper.IsNumericType( nullableSByte.GetType() ) );
  181. Single? nullableSingle = 3.2f;
  182. Assert.True( TypeHelper.IsNumericType( nullableSingle.GetType() ) );
  183. UInt16? nullableUInt16 = 12;
  184. Assert.True( TypeHelper.IsNumericType( nullableUInt16.GetType() ) );
  185. UInt16? nullableUInt32 = 12;
  186. Assert.True( TypeHelper.IsNumericType( nullableUInt32.GetType() ) );
  187. UInt16? nullableUInt64 = 12;
  188. Assert.True( TypeHelper.IsNumericType( nullableUInt64.GetType() ) );
  189.  
  190. }
  191.  
  192. if (Microsoft.VisualBasic.Information.IsNumeric("5"))
  193. {
  194. //Do Something
  195. }
  196.  
  197. public static bool Isumeric (object Expression)
  198. {
  199. bool f;
  200. ufloat64 a;
  201. long l;
  202.  
  203. IConvertible iConvertible = null;
  204. if ( ((Expression is IConvertible)))
  205. {
  206. iConvertible = (IConvertible) Expression;
  207. }
  208.  
  209. if (iConvertible == null)
  210. {
  211. if ( ((Expression is char[])))
  212. {
  213. Expression = new String ((char[]) Expression);
  214. goto IL_002d; // hopefully inserted by optimizer
  215. }
  216. return 0;
  217. }
  218. IL_002d:
  219. TypeCode typeCode = iConvertible.GetTypeCode ();
  220. if ((typeCode == 18) || (typeCode == 4))
  221. {
  222. string str = iConvertible.ToString (null);
  223. try
  224. {
  225. if ( (StringType.IsHexOrOctValue (str, l)))
  226. {
  227. f = true;
  228. return f;
  229. }
  230. }
  231. catch (Exception )
  232. {
  233. f = false;
  234. return f;
  235. };
  236. return DoubleType.TryParse (str, a);
  237. }
  238. return Utils.IsNumericTypeCode (typeCode);
  239. }
  240.  
  241. internal static bool IsNumericType (Type typ)
  242. {
  243. bool f;
  244. TypeCode typeCode;
  245. if ( (typ.IsArray))
  246. {
  247. return 0;
  248. }
  249. switch (Type.GetTypeCode (typ))
  250. {
  251. case 3:
  252. case 6:
  253. case 7:
  254. case 9:
  255. case 11:
  256. case 13:
  257. case 14:
  258. case 15:
  259. return 1;
  260. };
  261. return 0;
  262. }
  263.  
  264. /// <summary>
  265. /// Determines whether the supplied object is a .NET numeric system type
  266. /// </summary>
  267. /// <param name="val">The object to test</param>
  268. /// <returns>true=Is numeric; false=Not numeric</returns>
  269. public static bool IsNumeric(ref object val)
  270. {
  271. if (val == null)
  272. return false;
  273.  
  274. // Test for numeric type, returning true if match
  275. if
  276. (
  277. val is double || val is float || val is int || val is long || val is decimal ||
  278. val is short || val is uint || val is ushort || val is ulong || val is byte ||
  279. val is sbyte
  280. )
  281. return true;
  282.  
  283. // Not numeric
  284. return false;
  285. }
  286.  
  287. ''// Return true if a type is a numeric type.
  288. Private Function IsNumericType(ByVal this As Type) As Boolean
  289. ''// All the numeric types have bits 11xx set whereas non numeric do not.
  290. ''// That is if you include char type which is 4(decimal) = 100(binary).
  291. If this.IsArray Then Return False
  292. If (Type.GetTypeCode(this) And &HC) > 0 Then Return True
  293. Return False
  294. End Function
  295.  
  296. public bool IsInteger(Type t)
  297. {
  298. int i;
  299. return t.IsValueType && int.TryParse(t.ToString(), out i);
  300. }
Add Comment
Please, Sign In to add comment