Пример #1
0
 static bool _IsNumericType(CodeTypeReference t)
 {
     return(CTE.Equals(_Byte, t) || CTE.Equals(_SByte, t) ||
            CTE.Equals(_Short, t) || CTE.Equals(_UShort, t) ||
            CTE.Equals(_Int, t) || CTE.Equals(_UInt, t) ||
            CTE.Equals(_Long, t) || CTE.Equals(_Ulong, t) ||
            CTE.Equals(_FloatType, t) || CTE.Equals(_DoubleType, t) ||
            CTE.Equals(_DecimalType, t));
 }
Пример #2
0
        static CodeTypeReference _PromoteType(CodeTypeReference x, CodeTypeReference y)
        {
            if ((_IsNumericType(x) || CTE.Equals(_Char, x)) && (_IsNumericType(y) || CTE.Equals(_Char, y)))
            {
                //If either operand is of type decimal, the other operand is converted to type decimal, or a compile-time error occurs if the other operand is of type float or double.
                if (CTE.Equals(x, _DecimalType))
                {
                    if (CTE.Equals(_FloatType, y))
                    {
                        throw new InvalidOperationException("Cannot convert float to decimal");
                    }
                    if (CTE.Equals(_DoubleType, y))
                    {
                        throw new InvalidOperationException("Cannot convert double to decimal");
                    }
                    return(new CodeTypeReference(typeof(decimal)));
                }
                else if (CTE.Equals(y, _DecimalType))
                {
                    if (CTE.Equals(_FloatType, x))
                    {
                        throw new InvalidOperationException("Cannot convert float to decimal");
                    }
                    if (CTE.Equals(_DoubleType, x))
                    {
                        throw new InvalidOperationException("Cannot convert double to decimal");
                    }
                    return(new CodeTypeReference(typeof(decimal)));
                }
                //Otherwise, if either operand is of type double, the other operand is converted to type double.
                if (CTE.Equals(x, _DoubleType) || CTE.Equals(y, _DoubleType))
                {
                    return(new CodeTypeReference(typeof(double)));
                }
                //Otherwise, if either operand is of type float, the other operand is converted to type float.
                if (CTE.Equals(x, _FloatType) || CTE.Equals(y, _FloatType))
                {
                    return(new CodeTypeReference(typeof(double)));
                }
                //Otherwise, if either operand is of type ulong, the other operand is converted to type ulong,
                // or a compile-time error occurs if the other operand is of type sbyte, short, int, or long.
                if (CTE.Equals(x, _Ulong))
                {
                    if (CTE.Equals(_SByte, y) ||
                        CTE.Equals(_Short, y) ||
                        CTE.Equals(_Int, y) ||
                        CTE.Equals(_Long, y))
                    {
                        throw new InvalidOperationException("Cannot convert signed type to ulong");
                    }
                    return(new CodeTypeReference(typeof(ulong)));
                }
                else if (CTE.Equals(y, _Ulong))
                {
                    if (CTE.Equals(_SByte, x) ||
                        CTE.Equals(_Short, x) ||
                        CTE.Equals(_Int, x) ||
                        CTE.Equals(_Long, x))
                    {
                        throw new InvalidOperationException("Cannot convert signed type to ulong");
                    }
                    return(new CodeTypeReference(typeof(ulong)));
                }
                //Otherwise, if either operand is of type long, the other operand is converted to type long.
                if (CTE.Equals(x, _Long) || CTE.Equals(y, _Long))
                {
                    return(new CodeTypeReference(typeof(long)));
                }
                //Otherwise, if either operand is of type uint and the other operand is of type sbyte, short, or int, both operands are converted to type long.
                if (CTE.Equals(x, _UInt))
                {
                    if (CTE.Equals(_SByte, y) ||
                        CTE.Equals(_Short, y) ||
                        CTE.Equals(_Int, y))
                    {
                        return(new CodeTypeReference(typeof(long)));
                    }
                }
                else if (CTE.Equals(y, _UInt))
                {
                    if (CTE.Equals(_SByte, x) ||
                        CTE.Equals(_Short, x) ||
                        CTE.Equals(_Int, x))
                    {
                        return(new CodeTypeReference(typeof(long)));
                    }
                }
                //Otherwise, if either operand is of type uint, the other operand is converted to type uint.
                if (CTE.Equals(x, _UInt) || CTE.Equals(y, _UInt))
                {
                    return(new CodeTypeReference(typeof(uint)));
                }

                //Otherwise, both operands are converted to type int.
                return(new CodeTypeReference(typeof(int)));
            }
            if (CTE.Equals(x, y))
            {
                return(x);
            }
            throw new InvalidCastException("Cannot promote these types");
        }