使用window.external JavaScript的Date对象传递到C#WebBrowser控件控件、对象、external、window

2023-09-06 14:20:25 作者:坚持,梦在未来绽放

我能够调用 WebBrowser.ObjectForScripting 用JavaScript C#方法 window.external web浏览器的WinForms控制,并通过字符串 INT 布尔等,但我不知道如何通过JavaScript的日期对象,并以某种方式转换/元帅到.NET 日期时间类。

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.

很显然,我可以通过一个字符串并解析它,但是这是真的不是重点。我只是好奇,我怎么能做到这一点的JavaScript 日期和.NET 的DateTime

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?

推荐答案

我终于得到了它的工作。我不知道为什么,但我无法使用它与动态。这里是解决方案:

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);
    }
}