I have a function that, among other things, takes an object and a type and converts the object to that type. However, the input object is often double, and the type is some int change (uint, long, etc.). I want this to work if the round number is passed as double (e.g. 4.0), but to throw an exception if the decimal number is specified in (4.3). Is there an even more elegant way to check if a type is some int function?
if (inObject is double && (targetType == typeof (int) || targetType == typeof (uint) || targetType == typeof (long) || targetType == typeof (ulong) || targetType == typeof (short) || targetType == typeof (ushort))) { double input = (double) inObject; if (Math.Truncate(input) != input) throw new ArgumentException("Input was not an integer."); }
Thanks.
source share