0

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 throws NotImplementedException

8749236
  • 444
  • 7
  • 12
  • Does this answer your question? [Setting an object from .NET to JavaScript code through WebView2](https://stackoverflow.com/questions/62794609/setting-an-object-from-net-to-javascript-code-through-webview2) – Poul Bak Feb 19 '22 at 21:04
  • Here's another example: https://learn.microsoft.com/da-dk/dotnet/api/microsoft.web.webview2.core.corewebview2.addhostobjecttoscript?view=webview2-dotnet-1.0.1108.44#microsoft-web-webview2-core-corewebview2-addhostobjecttoscript(system-string-system-object) – Poul Bak Feb 19 '22 at 21:32
  • No. I am not trying to `addHostObjectToScript()`. I already did that. I have added a host object with a method `TestCall`. I am trying to pass JavaScript object to it like this `hostobj.TestCall(1234, { item1: 1111, item2: 2222 });`. My problem is I cannot get the data in supplied JavaScript object in C# – 8749236 Feb 20 '22 at 16:06
  • Try this: `public dynamic[] TestCall(string arg1, dynamic arg2)` (the `object` type does not have those proerties). – Poul Bak Feb 20 '22 at 18:17
  • Or (better), create a class with those properties. – Poul Bak Feb 20 '22 at 18:19
  • `'arg2.num' threw an exception of type 'System.NotImplementedException'`. Result is same as casting object to dynamic – 8749236 Feb 23 '22 at 03:28
  • `arg2.num` is not part of the object you send. – Poul Bak Feb 23 '22 at 17:08
  • Sorry, I use different variable names. However, it makes no difference, still end up with NotImplementedException. `arg2.item1' threw an exception of type 'System.NotImplementedException` – 8749236 Feb 24 '22 at 18:13
  • 1
    Under the section 'Passing Objects to .NET' under https://weblog.west-wind.com/posts/2021/Jan/26/Chromium-WebView2-Control-and-NET-to-JavaScript-Interop-Part-2 , the article suggests this isn't possible. But, obviously, both the article and this question are dated. – ianmac45 Dec 08 '22 at 17:18
  • @ianmac45 I used JSON as work around, as long as it is not performance critical, it will work fine. – 8749236 Dec 11 '22 at 18:08

0 Answers0