internal static async Task <TRes> MyInvokeAsync <TRes>( this IJSRuntime jsRuntime, string identifier, params object[] args) { var jsFriendlyArgs = MakeArgJsFriendly(jsRuntime, args); if (typeof(IJsObjectRef).IsAssignableFrom(typeof(TRes))) { var guid = await jsRuntime.InvokeAsync <string>(identifier, jsFriendlyArgs); return((TRes)JsObjectRefInstances.GetInstance(guid)); } if (typeof(IOneOf).IsAssignableFrom(typeof(TRes))) { var resultObject = await jsRuntime.InvokeAsync <string>(identifier, jsFriendlyArgs); object result = null; if (resultObject is string someText) { try { var jo = JObject.Parse(someText); if (jo.ContainsKey("dotnetTypeName")) { var typeName = jo.SelectToken("dotnetTypeName").Value <string>(); var asm = typeof(Map).Assembly; var type = asm.GetType(typeName); result = jo.ToObject(type); } else { result = someText; } } catch { result = someText; } } return((TRes)result); } else { return(await jsRuntime.InvokeAsync <TRes>(identifier, jsFriendlyArgs)); } }
internal static async Task <TRes> MyInvokeAsync <TRes>( this IJSRuntime jsRuntime, string identifier, params object[] args) { var jsFriendlyArgs = args .Select(arg => { if (arg == null) { return(arg); } if (arg is IOneOf oneof) { arg = oneof.Value; } var argType = arg.GetType(); switch (arg) { case ElementReference _: case string _: case int _: case long _: case double _: case float _: case decimal _: case DateTime _: case bool _: return(arg); case Action action: return(DotNetObjectReference.Create(new JsCallableAction(jsRuntime, action))); default: { if (argType.IsGenericType && (argType.GetGenericTypeDefinition() == typeof(Action <>))) { var genericArguments = argType.GetGenericArguments(); //Debug.WriteLine($"Generic args : {genericArguments.Count()}"); return(DotNetObjectReference.Create(new JsCallableAction(jsRuntime, (Delegate)arg, genericArguments))); } switch (arg) { case JsCallableAction _: return(DotNetObjectReference.Create(arg)); case IJsObjectRef jsObjectRef: { //Debug.WriteLine("Serialize IJsObjectRef"); var guid = jsObjectRef.Guid; return(SerializeObject(new JsObjectRef1(guid))); } default: return(SerializeObject(arg)); } } } }); if (typeof(IJsObjectRef).IsAssignableFrom(typeof(TRes))) { var guid = await jsRuntime.InvokeAsync <string>(identifier, jsFriendlyArgs); return((TRes)JsObjectRefInstances.GetInstance(guid)); } if (typeof(IOneOf).IsAssignableFrom(typeof(TRes))) { var resultObject = await jsRuntime.InvokeAsync <string>(identifier, jsFriendlyArgs); object result = null; if (resultObject is string someText) { try { var jo = JObject.Parse(someText); if (jo.ContainsKey("dotnetTypeName")) { var typeName = jo.SelectToken("dotnetTypeName").Value <string>(); var asm = typeof(Map).Assembly; var type = asm.GetType(typeName); result = jo.ToObject(type); } else { result = someText; } } catch { result = someText; } } return((TRes)result); } else { return(await jsRuntime.InvokeAsync <TRes>(identifier, jsFriendlyArgs)); } }