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
Solution 1:
You can take advantage of the fact that dates in Javascript are expressed as the number of milliseconds elapsed since the UNIX epoch (1970-01-01 00:00:00 UTC). Use the valueOf() method to obtain that value from a Date
object :
var millisecondsSinceEpoch = yourDate.valueOf();
If you can marshal that value to C# as a double
, you can create the appropriate DateTime
object using its constructor and the AddMilliseconds() method:
DateTimeyourDateTime=newDateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
.AddMilliseconds(millisecondsSinceEpoch);
Solution 2:
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)]
publicclassFoo
{
publicvoidBar(object o)
{
var dateTime = (DateTime)o.GetType().InvokeMember("getVarDate", BindingFlags.InvokeMethod, null, o, null);
Console.WriteLine(dateTime);
}
}
Solution 3:
I found better solution for my project.
in javascript:
var date = newDate();
myComObject.DoSomething(date.getVarDate());
in C#
[ComVisible(true)]
publicclassFoo
{
publicvoidBar(DateTime dateTime){
Console.WriteLine(dateTime);
}
}
Post a Comment for "Passing Javascript Date Object To C# Webbrowser Control Using Window.external"