Example #1
0
        /// <summary>Returns a <see langword="Long" /> value that corresponds to the specified object. </summary>
        /// <param name="Value">Required. Object to convert to a <see langword="Long" /> value.</param>
        /// <returns>The <see langword="Long" /> value that corresponds to <paramref name="Value" />.</returns>
        public static long FromObject(object Value)
        {
            if (Value == null)
            {
                return(0);
            }
            IConvertible ValueInterface = Value as IConvertible;

            if (ValueInterface != null)
            {
                switch (ValueInterface.GetTypeCode())
                {
                case TypeCode.Boolean:
                    return((long)-(ValueInterface.ToBoolean((IFormatProvider)null) ? 1 : 0));

                case TypeCode.Byte:
                    if (Value is byte)
                    {
                        return((long)(byte)Value);
                    }
                    return((long)ValueInterface.ToByte((IFormatProvider)null));

                case TypeCode.Int16:
                    if (Value is short)
                    {
                        return((long)(short)Value);
                    }
                    return((long)ValueInterface.ToInt16((IFormatProvider)null));

                case TypeCode.Int32:
                    if (Value is int)
                    {
                        return((long)(int)Value);
                    }
                    return((long)ValueInterface.ToInt32((IFormatProvider)null));

                case TypeCode.Int64:
                    if (Value is long)
                    {
                        return((long)Value);
                    }
                    return(ValueInterface.ToInt64((IFormatProvider)null));

                case TypeCode.Single:
                    if (Value is float)
                    {
                        return(checked ((long)Math.Round((double)(float)Value)));
                    }
                    return(checked ((long)Math.Round((double)ValueInterface.ToSingle((IFormatProvider)null))));

                case TypeCode.Double:
                    if (Value is double)
                    {
                        return(checked ((long)Math.Round((double)Value)));
                    }
                    return(checked ((long)Math.Round(ValueInterface.ToDouble((IFormatProvider)null))));

                case TypeCode.Decimal:
                    return(LongType.DecimalToLong(ValueInterface));

                case TypeCode.String:
                    return(LongType.FromString(ValueInterface.ToString((IFormatProvider)null)));
                }
            }
            throw new InvalidCastException(Utils.GetResourceString("InvalidCast_FromTo", Utils.VBFriendlyName(Value), "Long"));
        }