Using WebBrowser on a Form and calling into C# from Javascript with window.external. Passing in a Javascript array of classes in the function:
var x = [];
x.push(classa);
x.push(classa);
window.external.CSharpFunction(x);
I can successfully get x.length in C# with:
int length=(int)x.GetType().InvokeMember("length", BindingFlags.GetProperty, null, x, null);
My question is how do I get x[0] and x[1]?
I’ve tried
x.GetType().InvokeMember(string.Empty, BindingFlags.GetProperty, null, x, new object[1]{1});
and
x.GetType().InvokeMember(string.Empty, BindingFlags.InvokeMethod | BindingFlags.Default, null, x, new object[1]{0});
both of which fire off errors within the WebBrowser control.
Thanks
Please see this SO question:
Accessing properties of javascript objects using type dynamic in C# 4
I’ve also tried to retrieve values from properties on objects in the array without success. Retrieving simple values in the array, such as strings and integers is easy, but have had no luck with complex objects.
Your best bet (IMO) is to implement a similar solution as the one in the above SO question. Instead of passing the array directly, build a Javascript method that can be invoked from C# that will return the object in the array at the specified index. Like so:
C#
HTML / JavaScript