Beispiel #1
0
        private static object ExplicitCastToValueType(object o, Type to)
        {
            Debug.Assert(o != null);
            Debug.Assert(to.IsValueType());

            if (to == Int32Type)
            {
                return(ScriptingRuntimeHelpers.Int32ToObject(ExplicitCastToInt32(o)));
            }
            if (to == DoubleType)
            {
                return(ExplicitCastToDouble(o));
            }
            if (to == BooleanType)
            {
                return(ScriptingRuntimeHelpers.BooleanToObject(ExplicitCastToBoolean(o)));
            }
            if (to == ByteType)
            {
                return(ExplicitCastToByte(o));
            }
            if (to == CharType)
            {
                return(ExplicitCastToChar(o));
            }
            if (to == DecimalType)
            {
                return(ExplicitCastToDecimal(o));
            }
            if (to == Int16Type)
            {
                return(ExplicitCastToInt16(o));
            }
            if (to == Int64Type)
            {
                return(ExplicitCastToInt64(o));
            }
            if (to == SByteType)
            {
                return(ExplicitCastToSByte(o));
            }
            if (to == SingleType)
            {
                return(ExplicitCastToSingle(o));
            }
            if (to == UInt16Type)
            {
                return(ExplicitCastToUInt16(o));
            }
            if (to == UInt32Type)
            {
                return(ExplicitCastToUInt32(o));
            }
            if (to == UInt64Type)
            {
                return(ExplicitCastToUInt64(o));
            }

            if (to == NullableBooleanType)
            {
                return(ExplicitCastToNullableBoolean(o));
            }
            if (to == NullableByteType)
            {
                return(ExplicitCastToNullableByte(o));
            }
            if (to == NullableCharType)
            {
                return(ExplicitCastToNullableChar(o));
            }
            if (to == NullableDecimalType)
            {
                return(ExplicitCastToNullableDecimal(o));
            }
            if (to == NullableDoubleType)
            {
                return(ExplicitCastToNullableDouble(o));
            }
            if (to == NullableInt16Type)
            {
                return(ExplicitCastToNullableInt16(o));
            }
            if (to == NullableInt32Type)
            {
                return(ExplicitCastToNullableInt32(o));
            }
            if (to == NullableInt64Type)
            {
                return(ExplicitCastToNullableInt64(o));
            }
            if (to == NullableSByteType)
            {
                return(ExplicitCastToNullableSByte(o));
            }
            if (to == NullableSingleType)
            {
                return(ExplicitCastToNullableSingle(o));
            }
            if (to == NullableUInt16Type)
            {
                return(ExplicitCastToNullableUInt16(o));
            }
            if (to == NullableUInt32Type)
            {
                return(ExplicitCastToNullableUInt32(o));
            }
            if (to == NullableUInt64Type)
            {
                return(ExplicitCastToNullableUInt64(o));
            }

            if (to.IsAssignableFrom(o.GetType()))
            {
                return(o);
            }

            throw new InvalidCastException();
        }