private TiResponse staticProperty(TiRequestParams data)
        {
            if (!data.ContainsKey("className"))
            {
                throw new ReflectionException("\"staticProperty\" request missing \"className\" param");
            }

            string className = (string)data["className"];
            var    classType = InstanceRegistry.lookupType(className);

            if (classType == null)
            {
                throw new ReflectionException("\"staticProperty\" request invalid classname \"" + className + "\"");
            }

            if (!data.ContainsKey("property"))
            {
                throw new ReflectionException("\"staticProperty\" request missing \"property\" param");
            }

            PropertyInfo propertyInfo = classType.GetProperty((string)data["property"]);

            if (propertyInfo == null)
            {
                throw new ReflectionException("\"staticProperty\" request invalid property \"" + data["property"] + "\"");
            }

            object val = propertyInfo.GetValue(null);

            return(InstanceRegistry.createReturnType(val));
        }
        private TiResponse getEnum(TiRequestParams data)
        {
            if (!data.ContainsKey("name"))
            {
                throw new ReflectionException("\"getEnum\" request missing \"name\" param");
            }

            if (!data.ContainsKey("value"))
            {
                throw new ReflectionException("\"getEnum\" request missing \"value\" param");
            }

            string name  = (string)data["name"];
            string value = (string)data["value"];

            Type t = InstanceRegistry.lookupType(name);

            if (t == null)
            {
                throw new ReflectionException("\"getEnum\" request failed because \"" + name + "\" is an invalid class name");
            }

            object val = null;

            if (t.IsEnum)
            {
                val = Enum.Parse(t, value);
            }
            else
            {
                var prop = t.GetProperty(value);
                if (prop != null)
                {
                    val = prop.GetValue(null, null);
                }
            }

            return(InstanceRegistry.createReturnType(val));
        }
        private TiResponse invokeStaticAsync(TiRequestParams data)
        {
            if (!data.ContainsKey("className"))
            {
                throw new ReflectionException("\"invokeStatic\" request missing \"className\" param");
            }

            string className = (string)data["className"];
            var    classType = InstanceRegistry.lookupType(className);

            if (classType == null)
            {
                throw new ReflectionException("\"invokeStatic\" request invalid classname \"" + className + "\"");
            }

            if (!data.ContainsKey("method"))
            {
                throw new ReflectionException("\"invokeStatic\" request missing \"method\" param");
            }

            if (!data.ContainsKey("args"))
            {
                throw new ReflectionException("\"invokeStatic\" request missing \"args\" param");
            }

            JArray args = (JArray)data["args"];

            if (args.Count % 2 != 0)
            {
                throw new ReflectionException("\"invokeStatic\" request arguments must contain an even number of type-values");
            }

            // create the argument types array
            Type[] fnArgumentTypes = new Type[args.Count / 2];
            for (int i = 0, j = 0; i < args.Count; i += 2, j++)
            {
                fnArgumentTypes[j] = InstanceRegistry.lookupType((string)args[i]);
            }

            // get the method info
            MethodInfo methodInfo = classType.GetMethod((string)data["method"], fnArgumentTypes);

            // create the argument values array
            object[] fnArguments = new object[args.Count / 2];
            for (int i = 1, j = 0; i < args.Count; i += 2, j++)
            {
                JObject arg = (JObject)args[i];
                if (arg["valueHnd"] != null)
                {
                    fnArguments[j] = InstanceRegistry.getInstance((string)arg["valueHnd"]);
                }
                else if (fnArgumentTypes[j] == typeof(Uri))
                {
                    fnArguments[j] = new Uri((string)arg["valuePrimitive"], UriKind.RelativeOrAbsolute);
                }
                else
                {
                    fnArguments[j] = ((JValue)arg["valuePrimitive"]).Value;
                }
                if (fnArguments[j] != null)
                {
                    IConvertible convertible = fnArguments[j] as IConvertible;
                    if (convertible != null)
                    {
                        fnArguments[j] = Convert.ChangeType(fnArguments[j], fnArgumentTypes[j]);
                    }
                }
            }

            TiResponse  response = new TiResponse();
            InvokeAsync ia       = new InvokeAsync();

            response["handle"] = InstanceRegistry.createHandle(ia);
            ia.run(null, methodInfo, fnArguments);

            return(response);
        }
        private TiResponse invokeMethod(TiRequestParams data)
        {
            if (!data.ContainsKey("handle"))
            {
                throw new ReflectionException("\"invokeMethod\" request missing \"handle\" param");
            }

            string handle   = (string)data["handle"];
            object instance = InstanceRegistry.getInstance(handle);

            if (instance == null)
            {
                throw new ReflectionException("\"invokeMethod\" request invalid handle \"" + handle + "\"");
            }

            if (!data.ContainsKey("method"))
            {
                throw new ReflectionException("\"invokeMethod\" request missing \"method\" param");
            }

            if (!data.ContainsKey("args"))
            {
                throw new ReflectionException("\"invokeMethod\" request missing \"args\" param");
            }

            JArray args = (JArray)data["args"];

            if (args.Count % 2 != 0)
            {
                throw new ReflectionException("\"invokeMethod\" request arguments must contain an even number of type-values");
            }

            // create the argument types array
            Type[] fnArgumentTypes = new Type[args.Count / 2];
            for (int i = 0, j = 0; i < args.Count; i += 2, j++)
            {
                fnArgumentTypes[j] = InstanceRegistry.lookupType((string)args[i]);
            }

            // get the method info
            MethodInfo methodInfo = instance.GetType().GetMethod((string)data["method"], fnArgumentTypes);

            if (methodInfo.ReturnType.GetInterfaces().Contains(typeof(IAsyncInfo)))
            {
                throw new Exception("Use invokeMethodAsync() to call this method");
            }

            // create the argument values array
            object[] fnArguments = new object[args.Count / 2];
            for (int i = 1, j = 0; i < args.Count; i += 2, j++)
            {
                JObject arg = (JObject)args[i];
                if (arg["valueHnd"] != null)
                {
                    fnArguments[j] = InstanceRegistry.getInstance((string)arg["valueHnd"]);
                }
                else if (fnArgumentTypes[j] == typeof(Uri))
                {
                    fnArguments[j] = new Uri((string)arg["valuePrimitive"], UriKind.RelativeOrAbsolute);
                }
                else
                {
                    fnArguments[j] = ((JValue)arg["valuePrimitive"]).Value;
                }
                if (fnArguments[j] != null)
                {
                    IConvertible convertible = fnArguments[j] as IConvertible;
                    if (convertible != null)
                    {
                        fnArguments[j] = Convert.ChangeType(fnArguments[j], fnArgumentTypes[j]);
                    }
                }
            }

            // invoke the method
            var result = methodInfo.Invoke(instance, fnArguments);

            if (methodInfo.ReturnType == typeof(void))
            {
                result = null;
            }

            return(InstanceRegistry.createReturnType(result));
        }
        private TiResponse createInstance(TiRequestParams data)
        {
            if (!data.ContainsKey("className"))
            {
                throw new ReflectionException("\"createInstance\" request missing \"className\" param");
            }

            string className = (string)data["className"];
            var    classType = InstanceRegistry.lookupType(className);

            if (classType == null)
            {
                throw new ReflectionException("\"createInstance\" request invalid classname \"" + classType + "\"");
            }

            if (!data.ContainsKey("args"))
            {
                throw new ReflectionException("\"createInstance\" request missing \"args\" param");
            }

            JArray args = (JArray)data["args"];

            if (args.Count % 2 != 0)
            {
                throw new ReflectionException("\"createInstance\" request arguments must contain an even number of type-values");
            }

            // create the argument types array
            Type[] ctorArgumentTypes = new Type[args.Count / 2];
            for (int i = 0, j = 0; i < args.Count; i += 2, j++)
            {
                ctorArgumentTypes[j] = InstanceRegistry.lookupType((string)args[i]);
            }

            // create the argument values array
            object[] ctorArguments = new object[args.Count / 2];
            for (int i = 1, j = 0; i < args.Count; i += 2, j++)
            {
                JObject arg = (JObject)args[i];
                if (arg["valueHnd"] != null)
                {
                    ctorArguments[j] = InstanceRegistry.getInstance((string)arg["valueHnd"]);
                }
                else if (ctorArgumentTypes[j] == typeof(Uri))
                {
                    ctorArguments[j] = new Uri((string)arg["valuePrimitive"], UriKind.RelativeOrAbsolute);
                }
                else
                {
                    ctorArguments[j] = ((JValue)arg["valuePrimitive"]).Value;
                }
                if (ctorArguments[j] != null)
                {
                    ctorArguments[j] = Convert.ChangeType(ctorArguments[j], ctorArgumentTypes[j]);
                }
            }

            // Invoke the constructor and return the result
            var instance = classType.GetConstructor(ctorArgumentTypes).Invoke(ctorArguments);

            TiResponse response = new TiResponse();

            response["handle"] = InstanceRegistry.createHandle(instance);
            return(response);
        }