ToObject() public method

public ToObject ( ) : object
return object
示例#1
0
 static DateTime ParseDate(JsInstance obj)
 {
     DateTime result;
     if (!TryParseDate(obj, out result))
         throw new JsGlobalException(
             "Invalid cast '{0}' to 'DateTime'. Js object: {1}".Formatting(obj.ToObject(), obj));
     return result;
 }
示例#2
0
        static bool TryParseDate(JsInstance arg, out DateTime result)
        {
            object obj = arg.ToObject();
            string input = obj as string;
            if (input != null)
            {
                if (!DateTime.TryParse(input, out result))
                    if (!DateTime.TryParse(input, CultureInfo.InvariantCulture, DateTimeStyles.None, out result))
                        if (!DateTime.TryParseExact(input, "yyyyMMddHHmmss", CultureInfo.CurrentCulture, DateTimeStyles.None, out result))
                            if (!DateTime.TryParseExact(input, "yyyyMMdd", CultureInfo.CurrentCulture, DateTimeStyles.None, out result))
                                return false;
                return true;
            }
            else if (obj is DateTime)
            {
                result = (DateTime)obj;
                return true;
            }

            result = DateTime.MinValue;
            return false;
        }
示例#3
0
        static bool TryParseNum(JsInstance arg, out double result)
        {
            object obj = arg.ToObject();
            if (obj is string)
            {
                var input = (string)obj;

                if (input.Trim() == "-" || input.Trim() == ".")
                {
                    result = 0;
                    return true;
                }

                if (!double.TryParse(input, out result))
                    if (!double.TryParse(input, NumberStyles.Any, CultureInfo.InvariantCulture, out result))
                        return false;
                return true;
            }
            
            if (obj is bool)
            {
                result = (bool)obj ? 1 : 0;
                return true;
            }
            
            try
            {
                result = (double)Convert.ChangeType(obj, typeof(double));
                return true;
            }
            catch (InvalidCastException)
            {
                result = double.NaN;
                return false;
            }
        }
示例#4
0
        static double ParseDouble(JsInstance obj)
        {
            double result;
            if (!TryParseNum(obj, out result))
                throw new JsGlobalException(
                    "Invalid cast '{0}' to 'Int'. Js object: {1}".Formatting(obj.ToObject(), obj));

            if (double.IsNaN(result) || double.IsInfinity(result))
                throw new JsGlobalException("Argument is NaN or Infinity");
            return result;
        }
示例#5
0
 static int ParseInt(JsInstance obj)
 {
     double result;
     if (!TryParseNum(obj, out result))
         throw new JsGlobalException(
             "Invalid cast '{0}' to 'Int'. Js object: {1}".Formatting(obj.ToObject(), obj));
     return (int)result;
 }
示例#6
0
 static bool TryParseString(JsInstance obj, out string result)
 {
     result = obj.ToObject() as string;
     return result != null;
 }
示例#7
0
        static bool TryParseBool(JsInstance arg, out bool result)
        {
            object obj = arg.ToObject();
            if (obj is string)
            {
                string input = (string)obj;

                if (!bool.TryParse(input, out result))
                    return false;
            }
            else if (obj is bool)
            {
                result = (bool)obj;
            }
            else
            {
                double input = ParseDouble(arg);
                if (input < 0)
                {
                    result = false;
                    return false;
                }

                result = input != 0;
            }
            return true;
        }