Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Oct 4th, 2013  |  syntax: None  |  size: 1.62 KB  |  views: 35  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. object o = 123456U;
  2. ulong l = (ulong) o; // fails
  3.        
  4. object o = 123456U;
  5. ulong l = (ulong) (uint) o; // succeeds
  6.        
  7. void foo(object o)
  8. {
  9. switch (Type.GetTypeCode(o.GetType()))
  10.    {
  11.       case TypeCode.UInt32:
  12.       case TypeCode.UInt64:
  13.       ulong l = (ulong) o;
  14.       RunUnsignedIntVersion(l);
  15.       break;
  16.       case TypeCode.Int32:
  17.       case TypeCode.Int64:
  18.       long n = (long) o;
  19.       RunSignedVersion(n);
  20.       break;
  21.    }
  22. }
  23.        
  24. foo(123456U);
  25. foo(123456);
  26.        
  27. case TypeCode.Int32:
  28.     RunSignedVersion((int) o);
  29.     break;
  30. case TypeCode.Int64:
  31.     long n = (long) o;
  32.     RunSignedVersion(n);
  33.     break;
  34.        
  35. void foo(object o)
  36. {
  37.    switch (Type.GetTypeCode(o.GetType()))
  38.    {
  39.       case TypeCode.UInt32:
  40.       case TypeCode.UInt64:
  41.           ulong l = Convert.ToUInt64(o);
  42.           RunUnsignedIntVersion(l);
  43.           break;
  44.       case TypeCode.Int32:
  45.       case TypeCode.Int64:
  46.           long n = Convert.ToInt64(o);
  47.           RunSignedVersion(n);
  48.           break;
  49.    }
  50. }
  51.        
  52. [CLSCompliant(false)]  
  53.     public static ulong ToUInt64(object value) {
  54.         return value == null? 0: ((IConvertible)value).ToUInt64(null);
  55.     }
  56.  
  57.     [CLSCompliant(false)]  
  58.     public static long ToInt64(object value) {
  59.         return value == null? 0: ((IConvertible)value).ToInt64(null);
  60.     }
  61.        
  62. case TypeCode.Int32:
  63.     RunSignedVersion((int) o);
  64.     break;
  65.   case TypeCode.Int64:
  66.     long n = (long) o;
  67.     RunSignedVersion(n);
  68.     break;
  69.        
  70. public static class ConversionExtensions
  71. {
  72.     public static ulong ToUInt64(this object value)
  73.     {
  74.         return ((IConvertible)value).ToUInt64();
  75.     }
  76. }
  77.        
  78. object o = 123456U;
  79. ulong l = o.ToUInt64();
clone this paste RAW Paste Data