protected virtual async Task <T> TryInvoke <T>(string name, params object[] parameters) { if (scriptObjectProxy != null) { var parmCategory = GetParmCategory <T>(); var parms = new { handle = Handle, function = name, category = (int)parmCategory, args = ScriptObjectUtilities.WrapScriptParms(parameters) }; var result = await WebSharp.Bridge.TryInvoke(parms); return((parmCategory == ScriptParmCategory.None) ? (T)result : GetReturnValue <T>(parmCategory, result)); } return(default(T)); }
public static bool DictionaryToScriptableType(IDictionary <string, object> parm, object obj) { var parmType = obj.GetType(); var success = false; if (parmType.IsAttributeDefined <ScriptableTypeAttribute>(false)) { var mappings = GetScriptMemberMappings(parmType, ScriptMemberMappingDirection.ScriptAliasToMember); foreach (var key in parm.Keys) { if (mappings.ContainsKey(key)) { var pi = parmType.GetProperty(mappings[key]); if (pi.SetMethod != null) { if (pi.PropertyType.IsEnum) { // Try to map enum string values to their enum types. var lowerKey = parm[key].ToString().ToLower(); foreach (var ev in Enum.GetNames(pi.PropertyType)) { if (ev.ToLower() == lowerKey) { pi.SetValue(obj, Enum.Parse(pi.PropertyType, ev)); } } } else { var scriptObject = parm[key] as IDictionary <string, object>; if (scriptObject != null) { if (scriptObject.ContainsKey("websharp_id")) { pi.SetValue(obj, AnonymousObjectToScriptObjectType(pi.PropertyType, parm[key])); } else { pi.SetValue(obj, AnonymousObjectToScriptableType(pi.PropertyType, parm[key])); } } else { if (ScriptObjectUtilities.IsTypeScriptableTypeArray(pi.PropertyType)) { var objArray = parm[key] as object[]; if (objArray != null) { var arrayType = pi.PropertyType.GetElementType(); var array = Array.CreateInstance(arrayType, objArray.Length); for (int a = 0; a < array.Length; a++) { array.SetValue(ScriptObjectHelper.AnonymousObjectToScriptableType(arrayType, objArray[a]), a); } pi.SetValue(obj, array); } } else { pi.SetValue(obj, parm[key]); } } } success = true; } } } } return(success); }