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 property(TiRequestParams data) { if (!data.ContainsKey("handle")) { throw new ReflectionException("\"property\" request missing \"handle\" param"); } string handle = (string)data["handle"]; object instance = InstanceRegistry.getInstance(handle); if (instance == null) { throw new ReflectionException("\"property\" request invalid handle \"" + handle + "\""); } if (!data.ContainsKey("name")) { throw new ReflectionException("\"property\" request missing \"name\" param"); } var obj = data["name"]; if (obj == null) { throw new ReflectionException("\"property\" request \"name\" is null"); } Type instanceType = instance.GetType(); Type propType = instanceType.GetType(); TiResponse response = new TiResponse(); response["value"] = null; switch (obj.GetType().ToString()) { case "Newtonsoft.Json.Linq.JArray": { // get multiple props JArray arr = (JArray)obj; Dictionary <string, TiResponse> value = new Dictionary <string, TiResponse>(); for (var i = 0; i < arr.Count; i++) { string propName = arr[i].ToString(); var propertyInfo = instanceType.GetProperty(propName); if (propertyInfo == null) { throw new ReflectionException("Invalid property \"" + propName + "\""); } object val = propertyInfo.GetValue(instance); value[propName] = InstanceRegistry.createReturnType(val); } response["value"] = value; return(response); } case "Newtonsoft.Json.Linq.JObject": { // set multiple props JObject props = (JObject)obj; foreach (JProperty prop in props.Properties()) { var propertyInfo = instanceType.GetProperty(prop.Name); if (propertyInfo == null) { throw new ReflectionException("Invalid property \"" + prop.Name + "\""); } JObject value = (JObject)prop.Value; if (value["valueHnd"] != null) { propertyInfo.SetValue(instance, InstanceRegistry.getInstance((string)value["valueHnd"])); } else if (value["valuePrimitive"] != null) { var valuePrimitive = value["valuePrimitive"]; if (propertyInfo.PropertyType == typeof(Uri)) { propertyInfo.SetValue(instance, Convert.ChangeType(new Uri(valuePrimitive.ToObject <string>(), UriKind.RelativeOrAbsolute), propertyInfo.PropertyType)); } else { propertyInfo.SetValue(instance, valuePrimitive.ToObject(propertyInfo.PropertyType)); } } } return(null); } case "System.String": { string name = (string)obj; var propertyInfo = instanceType.GetProperty(name); if (propertyInfo == null) { throw new ReflectionException("Invalid property \"" + name + "\""); } // setting a single prop if (data.ContainsKey("valueHnd") && data["valueHnd"] != null) { propertyInfo.SetValue(instance, InstanceRegistry.getInstance((string)data["valueHnd"])); return(null); } else if (data.ContainsKey("valuePrimitive")) { var valuePrimitive = data["valuePrimitive"]; if (propertyInfo.PropertyType == typeof(Uri)) { valuePrimitive = new Uri((string)valuePrimitive, UriKind.RelativeOrAbsolute); } propertyInfo.SetValue(instance, Convert.ChangeType(valuePrimitive, propertyInfo.PropertyType)); return(null); } // getting a single prop object val = propertyInfo.GetValue(instance); return(InstanceRegistry.createReturnType(val)); } } return(null); }
private TiResponse invokeStatic(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); 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(null, fnArguments); if (methodInfo.ReturnType == typeof(void)) { result = null; } return(InstanceRegistry.createReturnType(result)); }