Search code examples
c#javascript.netwebbrowser-controlcom-interop

Passing javascript Date object to C# WebBrowser control using window.external


I am able to call C# methods of WebBrowser.ObjectForScripting with javascript window.external from WebBrowser WinForms control and pass string, int, bool etc. However I don't know how to pass javascript Date object and somehow convert/marshal it to .NET DateTime class.

Obviously, I could pass a string and parse it, but this is really not the point. I'm just curious how could I do it with javascript Date and .NET DateTime?


Solution

  • I finally got it working. I don't know why, but I am unable to use it with dynamic. Here is the solution:

    [ComVisible(true)]
    public class Foo
    {
        public void Bar(object o)
        {
            var dateTime = (DateTime)o.GetType().InvokeMember("getVarDate", BindingFlags.InvokeMethod, null, o, null);
            Console.WriteLine(dateTime);
        }
    }