I am playing with Webview2 and tried to pass JavaScript object to C# method of registered Host Object, but I do not know how to retrieve its content.
public object[] TestCall(string arg1, object arg2)
{
// arg2.item1 // This don't work
// Including arg2 in array will return it back to JavaScript, so it is stored some where
return new List<object>() { "item1", "item2", 123, false, arg1, arg2 }.ToArray();
}
// await window.chrome.webview.hostObjects.dotNetObject.TestCall("Hello world", { item1: "value1", item2: 1234 });
I tried to look up official C# samples, but none demonstrated how to pass JavaScript object into method of HostObject.
I had worked around this by using JSON to exchange data between C# and JavaScript.
Yet I'm still puzzled.
My question:
How do I pass any JavaScript object into method of HostObject without stringify it to JSON?
Is there anyway to get content out of JS object I received in arg2
?
What I tried
I used
System.Runtime.InteropServices.Marshal.GetIDispatchForObject(arg2);
to get a pointer.But I do not know what to do with it, since I do not have knowledge of COM or IDispatch.
I tried casting it to
ExpandoObject
,Invalid cast
I tried casting it to
dynamic
, successful, but((dynamic)arg2).item1
throwsNotImplementedException