Advertisement
stefanpu

NumberTypesClass

Mar 3rd, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.50 KB | None | 0 0
  1.     class NumbersTypes
  2.     {
  3.         private static readonly List<Type> numberTypes = new List<Type>() {  typeof(byte), typeof(sbyte),
  4.         typeof(short), typeof(ushort), typeof(int), typeof(uint), typeof(long), typeof(ulong),
  5.         typeof(float),typeof(decimal), typeof(Double), typeof(BigInteger), typeof(Complex)};
  6.  
  7.         public static List<Type> Types
  8.         {
  9.             get
  10.             {
  11.                 return numberTypes;
  12.             }
  13.         }
  14.  
  15.         public static bool IsNumberType(Type item)
  16.         {
  17.             // The built-in number types(int, double e.t.c.) are part of the "mscorlib" assembly.
  18.             // 1. In that case the "GetType" method can be called that way: Type.GetType(item.Name);
  19.             // 2. In case the number is not part of "mscorlib" assembly(as are "BigInteger" and "Complex"),
  20.             // its fully qualified assembly name has to be taken.
  21.             // In both cases the "AssemblyQualifiedName" property will work fine
  22.             // (considering that we know the types in the "Main" method(at compile time?))
  23.             // See: http://msdn.microsoft.com/en-us/library/w3f99sx1.aspx for "Type.GetType"
  24.             // See: http://msdn.microsoft.com/en-us/library/system.type.assemblyqualifiedname.aspx
  25.             // for "Type.AssemblyQualifiedName" property.
  26.             string assemblyName = item.AssemblyQualifiedName;
  27.  
  28.  
  29.             Type itemType = Type.GetType(assemblyName);
  30.             return numberTypes.Contains(itemType);
  31.         }
  32.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement