private static TypeUsage ValidateAndAdjustCastTypes(TypeUsage toType, TypeUsage fromType, Type toClrType, Type fromClrType)
{
// only support primitives if real casting is involved
if (toType == null
|| !TypeSemantics.IsScalarType(toType)
|| !TypeSemantics.IsScalarType(fromType))
{
throw new NotSupportedException(Strings.ELinq_UnsupportedCast(DescribeClrType(fromClrType), DescribeClrType(toClrType)));
}
var fromTypeKind = Helper.AsPrimitive(fromType.EdmType).PrimitiveTypeKind;
var toTypeKind = Helper.AsPrimitive(toType.EdmType).PrimitiveTypeKind;
if (toTypeKind == PrimitiveTypeKind.Decimal)
{
// Can't figure out the right precision and scale for decimal, so only accept integer types
switch (fromTypeKind)
{
case PrimitiveTypeKind.Byte:
case PrimitiveTypeKind.Int16:
case PrimitiveTypeKind.Int32:
case PrimitiveTypeKind.Int64:
case PrimitiveTypeKind.SByte:
// adjust precision and scale to ensure sufficient width
toType = TypeUsage.CreateDecimalTypeUsage((PrimitiveType)toType.EdmType, 19, 0);
break;
default:
throw new NotSupportedException(Strings.ELinq_UnsupportedCastToDecimal);
}
}
return toType;
}