コード例 #1
0
        /// <summary>
        /// Converts a <see cref="JavaScriptValue"/> with a number type to host value.
        /// </summary>
        public object ToHostNumber(JavaScriptValue arg, Type toType)
        {
            // 32-bit Conversions
            if (toType == typeof(byte))
            {
                return((byte)arg.ToInt32());
            }
            if (toType == typeof(sbyte))
            {
                return((sbyte)arg.ToInt32());
            }
            if (toType == typeof(short))
            {
                return((short)arg.ToInt32());
            }
            if (toType == typeof(ushort))
            {
                return((ushort)arg.ToInt32());
            }
            if (toType == typeof(int))
            {
                return(arg.ToInt32());
            }
            if (toType == typeof(uint))
            {
                return((uint)arg.ToInt32());
            }

            // 64-bit Conversions
            if (toType == typeof(long))
            {
                return((long)arg.ToDouble());
            }
            if (toType == typeof(ulong))
            {
                return((ulong)arg.ToDouble());
            }
            if (toType == typeof(float))
            {
                return((float)arg.ToDouble());
            }
            if (toType == typeof(double))
            {
                return(arg.ToDouble());
            }
            if (toType == typeof(decimal))
            {
                return((decimal)arg.ToDouble());
            }

            // Other Conversions
            if (toType == typeof(string))
            {
                return(arg.ConvertToString().ToString());
            }
            if (toType == typeof(bool))
            {
                return(arg.ConvertToBoolean().ToBoolean());
            }

            // Last Attempt
            if (toType.IsAssignableFrom(typeof(double)))
            {
                return(arg.ToDouble());
            }

            throw new Exception($"Cannot convert javascript number to type: {toType}");
        }