Esempio n. 1
0
        private static object ConvertNumericalValue(ElementType elementType, object value)
        {
            object    naturalTypedValue = null;
            Exception innerEx;

            //
            if (value is Byte || value is Int16 || value is Int32 || value is Int64 ||
                value is SByte || value is UInt16 || value is UInt32 || value is UInt64 ||
                value is Enum)
            {
                try {
                    IConvertible    cble = (IConvertible)value;
                    IFormatProvider fp   = System.Globalization.CultureInfo.InvariantCulture;
                    switch (elementType)
                    {
                    case ElementType.UInt8:
                        naturalTypedValue = cble.ToByte(fp);
                        break;

                    case ElementType.Int8:
                        naturalTypedValue = cble.ToSByte(fp);
                        break;

                    //--
                    case ElementType.UInt16:
                        naturalTypedValue = cble.ToUInt16(fp);
                        break;

                    case ElementType.Int16:
                        naturalTypedValue = cble.ToInt16(fp);
                        break;

                    //--
                    case ElementType.UInt64:
                        naturalTypedValue = cble.ToUInt64(fp);
                        break;

                    case ElementType.Int64:
                        naturalTypedValue = cble.ToInt64(fp);
                        break;

                    //--
                    case ElementType.UInt32:
                        naturalTypedValue = cble.ToUInt32(fp);
                        break;

                    default:
                        //case ElementType.Int32:
                        System.Diagnostics.Debug.Assert(elementType == ElementType.Int32, "Unexpected numeric type");
                        naturalTypedValue = cble.ToInt32(fp);
                        break;
                    }
                    return(naturalTypedValue);
                } catch (OverflowException ex) {
                    innerEx = ex;
                    //} catch (InvalidCastException ex) {
                    //    innerEx = ex;
                }
            }
            else
            {
                innerEx = null;
            }
            throw ServiceRecordParser.new_ArgumentOutOfRangeException(
                      String.Format(System.Globalization.CultureInfo.InvariantCulture,
                                    "Value '{1}'  of type '{2}' not valid for element type {0}.",
                                    elementType, value, value.GetType()), innerEx);
        }