I have a data reader. I want to compare the value in it with the value 42. I know it is an integral type (e.g., what MySQL calls INT
, TINYINT
, SMALLINT
, MEDIUMINT
, BIGINT
, JUMBODELUXEINT
, etc.). I do not want to hardwire the actual type in to the C# code. The best I have come up with is
object x = reader.GetValue(i);
uint k = x is byte ? (byte) x
: x is short ? (uint) (short) x
: x is ushort ? (ushort) x
: x is int ? (int) (int) x
: (uint) x;
if (k == 42) { ... }
This seems incredibly long-winded. I tried using Equals
but different integral types with the same value do not appear to test as equal.
Is there a better way?