public static bool IsIntegerInRangeOf(ICompileTimeConstant constExpression, ITypeReference targetType) {
switch (targetType.TypeCode) {
case PrimitiveTypeCode.UInt8: {
IConvertible/*?*/ ic = constExpression.Value as IConvertible;
if (ic == null) return false;
switch (ic.GetTypeCode()) {
case System.TypeCode.Byte:
return true;
case System.TypeCode.SByte:
return byte.MinValue <= ic.ToSByte(null);
case System.TypeCode.Int16:
short s = ic.ToInt16(null);
return byte.MinValue <= s && s <= byte.MaxValue;
case System.TypeCode.Int32:
int i = ic.ToInt32(null);
return byte.MinValue <= i && i <= byte.MaxValue;
case System.TypeCode.Int64:
long lng = ic.ToInt64(null);
return byte.MinValue <= lng && lng <= byte.MaxValue;
case System.TypeCode.UInt16:
return ic.ToUInt16(null) <= byte.MaxValue;
case System.TypeCode.UInt32:
return ic.ToUInt32(null) <= byte.MaxValue;
case System.TypeCode.UInt64:
return ic.ToUInt64(null) <= byte.MaxValue;
case System.TypeCode.Decimal:
decimal d = ic.ToDecimal(null);
return byte.MinValue <= d && d <= byte.MaxValue;
}
return false;
}
case PrimitiveTypeCode.UInt16: {
IConvertible/*?*/ ic = constExpression.Value as IConvertible;
if (ic == null) return false;
switch (ic.GetTypeCode()) {
case System.TypeCode.Byte:
case System.TypeCode.UInt16:
return true;
case System.TypeCode.SByte:
return ushort.MinValue <= ic.ToSByte(null);
case System.TypeCode.Int16:
return ushort.MinValue <= ic.ToInt16(null);
case System.TypeCode.Int32:
int i = ic.ToInt32(null);
return ushort.MinValue <= i && i <= ushort.MaxValue;
case System.TypeCode.Int64:
long lng = ic.ToInt64(null);
return ushort.MinValue <= lng && lng <= ushort.MaxValue;
case System.TypeCode.UInt32:
return ic.ToUInt32(null) <= ushort.MaxValue;
case System.TypeCode.UInt64:
return ic.ToUInt64(null) <= ushort.MaxValue;
case System.TypeCode.Decimal:
decimal d = ic.ToDecimal(null);
return ushort.MinValue <= d && d <= ushort.MaxValue;
}
return false;
}
case PrimitiveTypeCode.UInt32: {
IConvertible/*?*/ ic = constExpression.Value as IConvertible;
if (ic == null) return false;
switch (ic.GetTypeCode()) {
case System.TypeCode.Byte:
case System.TypeCode.UInt16:
case System.TypeCode.UInt32:
return true;
case System.TypeCode.SByte:
return uint.MinValue <= ic.ToSByte(null);
case System.TypeCode.Int16:
return uint.MinValue <= ic.ToInt16(null);
case System.TypeCode.Int32:
return uint.MinValue <= ic.ToInt32(null);
case System.TypeCode.Int64:
long lng = ic.ToInt64(null);
return uint.MinValue <= lng && lng <= uint.MaxValue;
case System.TypeCode.UInt64:
return ic.ToUInt64(null) <= uint.MaxValue;
case System.TypeCode.Decimal:
decimal d = ic.ToDecimal(null);
return uint.MinValue <= d && d <= uint.MaxValue;
}
return false;
}
case PrimitiveTypeCode.UInt64: {
IConvertible/*?*/ ic = constExpression.Value as IConvertible;
if (ic == null) return false;
switch (ic.GetTypeCode()) {
case System.TypeCode.Byte:
case System.TypeCode.UInt16:
case System.TypeCode.UInt32:
case System.TypeCode.UInt64:
return true;
case System.TypeCode.SByte:
return 0 <= ic.ToSByte(null);
case System.TypeCode.Int16:
return 0 <= ic.ToInt16(null);
case System.TypeCode.Int32:
return 0 <= ic.ToInt32(null);
case System.TypeCode.Int64:
return 0 <= ic.ToInt64(null);
case System.TypeCode.Decimal:
decimal d = ic.ToDecimal(null);
return 0 <= d && d <= ulong.MaxValue;
}
return false;
}
case PrimitiveTypeCode.Int8: {
IConvertible/*?*/ ic = constExpression.Value as IConvertible;
if (ic == null) return false;
switch (ic.GetTypeCode()) {
case System.TypeCode.SByte:
return true;
case System.TypeCode.Int16:
short s = ic.ToInt16(null);
return sbyte.MinValue <= s && s <= sbyte.MaxValue;
case System.TypeCode.Int32:
int i = ic.ToInt32(null);
return sbyte.MinValue <= i && i <= sbyte.MaxValue;
case System.TypeCode.Int64:
long lng = ic.ToInt64(null);
return sbyte.MinValue <= lng && lng <= sbyte.MaxValue;
case System.TypeCode.Byte:
return ic.ToByte(null) <= sbyte.MaxValue;
case System.TypeCode.UInt16:
return ic.ToUInt16(null) <= sbyte.MaxValue;
case System.TypeCode.UInt32:
return ic.ToUInt32(null) <= sbyte.MaxValue;
case System.TypeCode.UInt64:
return ic.ToUInt64(null) <= (ulong)sbyte.MaxValue;
case System.TypeCode.Decimal:
decimal d = ic.ToDecimal(null);
return sbyte.MinValue <= d && d <= sbyte.MaxValue;
}
return false;
}
case PrimitiveTypeCode.Int16: {
IConvertible/*?*/ ic = constExpression.Value as IConvertible;
if (ic == null) return false;
switch (ic.GetTypeCode()) {
case System.TypeCode.SByte:
case System.TypeCode.Byte:
case System.TypeCode.Int16:
return true;
case System.TypeCode.Int32:
int i = ic.ToInt32(null);
return short.MinValue <= i && i <= short.MaxValue;
case System.TypeCode.Int64:
long lng = ic.ToInt64(null);
return short.MinValue <= lng && lng <= short.MaxValue;
case System.TypeCode.UInt16:
return ic.ToUInt16(null) <= short.MaxValue;
case System.TypeCode.UInt32:
return ic.ToUInt32(null) <= short.MaxValue;
case System.TypeCode.UInt64:
return ic.ToUInt64(null) <= (ulong)short.MaxValue;
case System.TypeCode.Decimal:
decimal d = ic.ToDecimal(null);
return short.MinValue <= d && d <= short.MaxValue;
}
return false;
}
case PrimitiveTypeCode.Int32: {
IConvertible/*?*/ ic = constExpression.Value as IConvertible;
if (ic == null) return false;
switch (ic.GetTypeCode()) {
case System.TypeCode.SByte:
case System.TypeCode.Byte:
case System.TypeCode.Int16:
case System.TypeCode.UInt16:
case System.TypeCode.Int32:
return true;
case System.TypeCode.Int64:
long lng = ic.ToInt64(null);
return int.MinValue <= lng && lng <= int.MaxValue;
case System.TypeCode.UInt32:
return ic.ToUInt32(null) <= int.MaxValue;
case System.TypeCode.UInt64:
return ic.ToUInt64(null) <= int.MaxValue;
case System.TypeCode.Decimal:
decimal d = ic.ToDecimal(null);
return int.MinValue <= d && d <= int.MaxValue;
}
return false;
}
case PrimitiveTypeCode.Int64: {
IConvertible/*?*/ ic = constExpression.Value as IConvertible;
if (ic == null) return false;
switch (ic.GetTypeCode()) {
case System.TypeCode.SByte:
case System.TypeCode.Byte:
case System.TypeCode.Int16:
case System.TypeCode.UInt16:
case System.TypeCode.Int32:
case System.TypeCode.UInt32:
case System.TypeCode.Int64:
return true;
case System.TypeCode.UInt64:
return ic.ToUInt64(null) <= int.MaxValue;
case System.TypeCode.Decimal:
decimal d = ic.ToDecimal(null);
return long.MinValue <= d && d <= long.MaxValue;
}
return false;
}
}
return false;
}