private T getProperty <T>(FBXJSAPI api, string name) { fbxvariant jsvalue = new fbxvariant(); api.GetProperty(name, jsvalue); return(Converter.ToNet3 <T>(jsvalue)); }
public static fbxvariant FromNet2(object value) { fbxvariant result = new fbxvariant(); FromNet(value, result); return(result); }
// ----- SetProperty methods ------ FBXResult SetIndexedProperty(object idx, fbxvariant value) { object convertedValue; DefaultMemberAttribute defaultMember = (DefaultMemberAttribute)Attribute.GetCustomAttribute(type, typeof(DefaultMemberAttribute)); var property = (defaultMember != null) ? getProp(defaultMember.MemberName) : null; if (property != null) { FBXResult returnValue = Converter.ToNet(value, property.PropertyType.GetElementType(), out convertedValue); if (!returnValue.success) { return(returnValue); } try { property.SetValue(this.wrappedObject, value, new object[] { idx }); } catch (Exception e) { return(new FBXResult(false, e.ToString())); } return(FBXResult.successful); } return(new FBXResult(false, "Property '" + idx + "' not found")); }
public override FBXResult SetProperty(string propertyName, fbxvariant value) { object convertedValue; FieldInfo field = type.GetField(propertyName); if (field != null) { FBXResult returnValue = Converter.ToNet(value, field.FieldType, out convertedValue); if (!returnValue.success) { return(returnValue); } try { field.SetValue(this.wrappedObject, convertedValue); } catch (Exception e) { return(new FBXResult(false, e.ToString())); } return(returnValue); } PropertyInfo property = getProp(propertyName); if (property != null) { FBXResult returnValue = Converter.ToNet(value, property.PropertyType, out convertedValue); if (!returnValue.success) { return(returnValue); } try { property.SetValue(this.wrappedObject, convertedValue, null); } catch (Exception e) { return(new FBXResult(false, e.ToString())); } return(returnValue); } FBXResult result = SetIndexedProperty(propertyName, value); if (!result.success) { int idx; if (Int32.TryParse(propertyName, out idx)) { return(SetIndexedProperty(idx, value)); } } return(result); }
public override FBXResult GetProperty(string propertyName, fbxvariant value) { if (propertyName == "toString") { propertyName = "ToString"; } // possible types: all, constructor, custom, event, field, method, nested type, property, typeinfo object obj; FieldInfo field = type.GetField(propertyName); if (field != null) { try { obj = field.GetValue(this.wrappedObject); } catch (Exception e) { return(new FBXResult(false, e.ToString())); } return(Converter.FromNet(obj, value)); } var property = getProp(propertyName); if (property != null) { try { obj = property.GetValue(this.wrappedObject, null); } catch (Exception e) { return(new FBXResult(false, e.ToString())); } return(Converter.FromNet(obj, value)); } // todo: why not use Delegate.CreateDelegate() here? Because we need to convert parameters explicitly to the desired target types? // maybe we should get rid of variants and replace them with DynamicObjects instead. if (type.GetMethods().Any(method => method.Name == propertyName)) { return(Converter.FromNet(new MethodObject(this.wrappedObject, propertyName), value)); } FBXResult result = GetIndexedProperty(propertyName, value); if (!result.success) { int idx; if (Int32.TryParse(propertyName, out idx)) { return(GetIndexedProperty(idx, value)); } } return(result); }
public override FBXResult Construct(VariantVector args, fbxvariant returnValue) { FBXResult result = Converter.InvokeOverload(this.typeObject, this.typeObject.GetConstructors(), "*ctor*", args, returnValue); if (!result.success) return result; /*dynamic newInstance = new JSObject(returnValue.get_object()); newInstance._cs_prototype = this.typeObject;*/ return result; }
// Getting a property. public override bool TryGetMember(GetMemberBinder binder, out object result) { fbxvariant value = new fbxvariant(); FBXResult jsresult = wrappedObject.GetProperty(binder.Name, value); if (!jsresult.success) { throw new Exception(jsresult.message); } Converter.ToNet(value, typeof(object), out result); return(true); }
public override FBXResult Invoke(string methodName, VariantVector args, fbxvariant result) { if (methodName == "") { IEnumerable <MethodInfo> methods = from method in this.methodObject.obj.GetType().GetMethods() where method.Name == this.methodObject.name select method; return(Converter.InvokeOverload(this.methodObject.obj, methods, this.methodObject.name, args, result)); } else { throw new Exception("Internal error - execution should never reach here"); } }
public override FBXResult Construct(VariantVector args, fbxvariant returnValue) { FBXResult result = Converter.InvokeOverload(this.typeObject, this.typeObject.GetConstructors(), "*ctor*", args, returnValue); if (!result.success) { return(result); } /*dynamic newInstance = new JSObject(returnValue.get_object()); * newInstance._cs_prototype = this.typeObject;*/ return(result); }
// todo: refactor this into one function public static FBXResult ToNet(fbxvariant value, Type targetType, out Object result) { try { result = ToNet2(value, targetType); } catch (InvalidCastException e) { result = null; return(new FBXResult(false, "Cannot cast object of type '" + value + "' " + e)); } return(FBXResult.successful); }
public override FBXResult GetProperty(string propertyName, fbxvariant value) { if (propertyName == "toString") propertyName = "ToString"; // possible types: all, constructor, custom, event, field, method, nested type, property, typeinfo object obj; FieldInfo field = type.GetField(propertyName); if (field != null) { try { obj = field.GetValue(this.wrappedObject); } catch (Exception e) { return new FBXResult(false, e.ToString()); } return Converter.FromNet(obj, value); } var property = getProp(propertyName); if (property != null) { try { obj = property.GetValue(this.wrappedObject, null); } catch (Exception e) { return new FBXResult(false, e.ToString()); } return Converter.FromNet(obj, value); } // todo: why not use Delegate.CreateDelegate() here? Because we need to convert parameters explicitly to the desired target types? // maybe we should get rid of variants and replace them with DynamicObjects instead. if ( type.GetMethods().Any( method => method.Name == propertyName ) ) return Converter.FromNet(new MethodObject(this.wrappedObject, propertyName), value); FBXResult result = GetIndexedProperty(propertyName, value); if (!result.success) { int idx; if (Int32.TryParse(propertyName, out idx)) { return GetIndexedProperty(idx, value); } } return result; }
public static FBXResult InvokeOverload(Object obj, IEnumerable <MethodBase> candidates, string name, VariantVector args, fbxvariant result, out object resultObj) { resultObj = null; System.Collections.Generic.List <object> convertedArgs = new System.Collections.Generic.List <object>(); // todo: cache conversion results for more speed foreach (MethodBase candidate in candidates) { ParameterInfo[] targetParameters = candidate.GetParameters(); int count = Math.Min(args.Count(), targetParameters.Length); object[] converted = new object[count]; bool ok = true; for (int i = 0; i < count; i++) { fbxvariant sourceValue = args[i]; ParameterInfo targetParameter = targetParameters[i]; if (!ToNet(sourceValue, targetParameter.ParameterType, out converted[i]).success) { ok = false; break; } } if (ok) { try { // todo: this is ugly, factor out if (name != "*ctor*") { resultObj = candidate.Invoke(obj, converted); } else { resultObj = ((ConstructorInfo)candidate).Invoke(converted); } } catch (Exception e) { return(new FBXResult(false, "Error invoking '" + name + "'. Details: " + e)); } return(FromNet(resultObj, result)); } } return(new FBXResult(false, "Could not find matching overload for '" + name + "'")); }
public void testXMLHTTPRequest(FBXJSAPI xmlHttpClass) { // var req = new XMLHttpRequest(); fbxvariant jsxmlreq = new fbxvariant(); xmlHttpClass.Construct(new VariantVector(), jsxmlreq); FBXJSAPI req = Converter.ToNet3 <FBXJSAPI>(jsxmlreq); // req.open('GET', 'http://www.google.com', false); fbxvariant intermediateResult = new fbxvariant(); req.Invoke("open", Converter.FromNet2(new object[] { "GET", "./FBControl.htm", false }), intermediateResult); // req.send(null); req.Invoke("send", Converter.FromNet2(new object[] { null }), intermediateResult); // alert(req.responseText); MessageBox.Show(getProperty <string>(req, "responseText"), "content of http://www.google.com is..."); }
// Setting a property. public override bool TrySetMember(SetMemberBinder binder, object value) { fbxvariant convertedValue = new fbxvariant(); FBXResult jsresult = Converter.FromNet(value, convertedValue); if (!jsresult.success) { throw new Exception(jsresult.message); } jsresult = wrappedObject.SetProperty(binder.Name, convertedValue); if (!jsresult.success) { throw new Exception(jsresult.message); } return(true); }
public void showTitle() { //context.document.title = "Hello, this is the browser/js window"; //string title = context.js().window.eval("document.title"); fbxvariant jsdocument = new fbxvariant(); context.GetProperty("document", jsdocument); fbxvariant jstitle = new fbxvariant(); Converter.ToNet3 <FBXJSAPI>(jsdocument).GetProperty("title", jstitle); MessageBox.Show(Converter.ToNet3 <string>(jstitle), "document.title is..."); /*context.js().window.eval(@" * var obj = new MyClass(); * alert( obj.data ); * obj.ShowMessage('Hello from JS'); * ");*/ }
// ----- GetProperty methods ------ FBXResult GetIndexedProperty(object idx, fbxvariant value) { object obj; DefaultMemberAttribute defaultMember = (DefaultMemberAttribute)Attribute.GetCustomAttribute(type, typeof(DefaultMemberAttribute)); var property = (defaultMember != null) ? getProp(defaultMember.MemberName) : null; if (property != null) { try { obj = property.GetValue(this.wrappedObject, new object[] { idx }); } catch (Exception e) { return(new FBXResult(false, e.ToString())); } return(Converter.FromNet(obj, value)); } // need to check if this.wrappedObject can be indexed directly? return(new FBXResult(false, "Property '" + idx + "' not found")); }
public override FBXResult Invoke(string methodName, VariantVector args, fbxvariant result) { // Invoke default member here if methodName == ""? return(new FBXResult(false, "Method '" + methodName + "' not found")); }
// ----- GetProperty methods ------ FBXResult GetIndexedProperty(object idx, fbxvariant value) { object obj; DefaultMemberAttribute defaultMember = (DefaultMemberAttribute)Attribute.GetCustomAttribute(type, typeof(DefaultMemberAttribute)); var property = (defaultMember != null) ? getProp(defaultMember.MemberName) : null; if (property != null) { try { obj = property.GetValue(this.wrappedObject, new object[] { idx }); } catch (Exception e) { return new FBXResult(false, e.ToString()); } return Converter.FromNet(obj, value); } // need to check if this.wrappedObject can be indexed directly? return new FBXResult(false, "Property '" + idx + "' not found"); }
// ----- HasProperty methods ------ public override bool HasProperty(string propertyName) { fbxvariant temp = new fbxvariant(); return GetProperty(propertyName, temp).success; }
public override FBXResult GetProperty(int idx, fbxvariant value) { return(GetIndexedProperty(idx, value)); }
public override FBXResult SetProperty(string propertyName, fbxvariant value) { object convertedValue; FieldInfo field = type.GetField(propertyName); if (field != null) { FBXResult returnValue = Converter.ToNet(value, field.FieldType, out convertedValue); if (!returnValue.success) return returnValue; try { field.SetValue(this.wrappedObject, convertedValue); } catch (Exception e) { return new FBXResult(false, e.ToString()); } return returnValue; } PropertyInfo property = getProp(propertyName); if (property != null) { FBXResult returnValue = Converter.ToNet(value, property.PropertyType, out convertedValue); if (!returnValue.success) return returnValue; try { property.SetValue(this.wrappedObject, convertedValue, null); } catch (Exception e) { return new FBXResult(false, e.ToString()); } return returnValue; } FBXResult result = SetIndexedProperty(propertyName, value); if (!result.success) { int idx; if (Int32.TryParse(propertyName, out idx)) { return SetIndexedProperty(idx, value); } } return result; }
public static FBXResult InvokeOverload(Object obj, IEnumerable <MethodBase> candidates, string name, VariantVector args, fbxvariant result) { object resultObj; return(InvokeOverload(obj, candidates, name, args, result, out resultObj)); }
public static T ToNet3 <T>(fbxvariant value) { object result = ToNet2(value, typeof(T)); return((T)result); }
public override bool HasProperty(int idx) { fbxvariant temp = new fbxvariant(); return GetProperty(idx, temp).success; }
// ----- SetProperty methods ------ FBXResult SetIndexedProperty(object idx, fbxvariant value) { object convertedValue; DefaultMemberAttribute defaultMember = (DefaultMemberAttribute)Attribute.GetCustomAttribute(type, typeof(DefaultMemberAttribute)); var property = (defaultMember != null) ? getProp(defaultMember.MemberName) : null; if (property != null) { FBXResult returnValue = Converter.ToNet(value, property.PropertyType.GetElementType(), out convertedValue); if (!returnValue.success) return returnValue; try { property.SetValue(this.wrappedObject, value, new object[] { idx }); } catch (Exception e) { return new FBXResult(false, e.ToString()); } return FBXResult.successful; } return new FBXResult(false, "Property '" + idx + "' not found"); }
public override FBXResult Construct(VariantVector args, fbxvariant returnValue) { return new FBXResult(false, "Object has no constructor"); }
public override FBXResult GetProperty(int idx, fbxvariant value) { return GetIndexedProperty(idx, value); }
public static FBXResult FromNet(Object value, fbxvariant result) { if (value is bool) { result.set((bool)value); } else if (value is int) { result.set((int)value); } else if (value is uint) { result.set((uint)value); } else if (value is double) { result.set((double)value); } else if (value is float) { result.set((float)value); } else if (value is string) { result.set((string)value); } else if (value is short) { result.set((short)value); } else if (value is ushort) { result.set((ushort)value); } else if (value is char) { result.set((char)value); } else if (value is byte) { result.set((byte)value); } else if (value is Int64) { result.set((Int64)value); } else if (value is UInt64) { result.set((UInt64)value); } else if (value == null) { result.set_null(); } //else if (value == Empty) // result.set_empty(); else if (value is FBXJSAPI) { result.set((FBXJSAPI)value); } else if (value is JSObject) { result.set(((JSObject)value).wrappedObject); } else if (value is MethodObject) { result.set(new MethodJSAPI(value)); } else if (value is Type) { result.set(new TypeJSAPI(value)); } else if (value is Object) { result.set(new ObjectJSAPI(value)); } else { // throw bad cast exception here? return(new FBXResult(false, "Cannot cast '" + value.ToString() + "' to an fbx type")); } return(FBXResult.successful); }
public override FBXResult Construct(VariantVector args, fbxvariant returnValue) { return(new FBXResult(false, "Object has no constructor")); }
public static Object ToNet2(fbxvariant value, Type targetType) { switch (value.get_type()) { case "bool": if (targetType.IsAssignableFrom(typeof(bool))) { return(value.get_bool()); } break; case "int": if (targetType.IsAssignableFrom(typeof(int))) { return(value.get_int()); } break; case "uint": if (targetType.IsAssignableFrom(typeof(uint))) { return(value.get_uint()); } break; case "double": { Double v = value.get_double(); if (targetType.IsAssignableFrom(typeof(double))) { return(v); } if (targetType.IsAssignableFrom(typeof(int)) && (v == (int)v)) { return((int)v); } } break; case "float": if (targetType.IsAssignableFrom(typeof(float))) { return(value.get_float()); } break; case "wstring": if (targetType.IsAssignableFrom(typeof(string))) { return(value.get_wstring()); } break; case "short": if (targetType.IsAssignableFrom(typeof(short))) { return(value.get_short()); } break; case "ushort": if (targetType.IsAssignableFrom(typeof(ushort))) { return(value.get_ushort()); } break; case "char": if (targetType.IsAssignableFrom(typeof(char))) { return(value.get_char()); } break; case "uchar": if (targetType.IsAssignableFrom(typeof(byte))) { return(value.get_uchar()); } break; case "int64": if (targetType.IsAssignableFrom(typeof(Int64))) { return(value.get_int64()); } break; case "uint64": if (targetType.IsAssignableFrom(typeof(UInt64))) { return(value.get_uint64()); } break; case "null": if (targetType.IsAssignableFrom(typeof(Nullable))) { return(null); } break; case "empty": if (targetType.IsAssignableFrom(typeof(Nullable))) { return(null); } break; case "object": { FBXJSAPI v = value.get_object(); if (targetType.IsAssignableFrom(typeof(FBXJSAPI))) { return(v); } else { dynamic jsObj = new JSObject(v); int testInt = jsObj; Expression conversion = Expression.Convert(Expression.Constant(jsObj), targetType); Type delegateType = typeof(Func <>).MakeGenericType(targetType); Delegate conversionDelegate = Expression.Lambda(delegateType, conversion).Compile(); var result = conversionDelegate.DynamicInvoke(); return(result); } } } throw new InvalidCastException("Cannot cast object of type '" + value.get_type() + "' to type '" + targetType.Name + "'"); }
// ----- HasProperty methods ------ public override bool HasProperty(string propertyName) { fbxvariant temp = new fbxvariant(); return(GetProperty(propertyName, temp).success); }
public override bool HasProperty(int idx) { fbxvariant temp = new fbxvariant(); return(GetProperty(idx, temp).success); }
public override FBXResult Invoke(string methodName, VariantVector args, fbxvariant result) { // Invoke default member here if methodName == ""? return new FBXResult(false, "Method '" + methodName + "' not found"); }