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
Using Arrays Elements of an array are accessed by specifying the index ( offset ) of the desired element within square [ ] brackets after the array name. Array subscripts must be of integer type. ( int, long int, char, etc. )
An item in a JavaScript array is accessed by referring to the index number of the item in square brackets. We know 0 will always output the first item in an array. We can also find the last item in an array by performing an operation on the length property and applying that as the new index number.
Access Array Elements You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.
To access an element of the multidimensional array, you first use square brackets to access an element of the outer array that returns an inner array; and then use another square bracket to access the element of the inner array.
The items of an array are called elements. To access an array element, you have to write the array name, followed by square brackets and pass the index value of the element you want to access to the square brackets. In JavaScript, arrays are zero-based, which means the indexing of elements starts from 0.
When to Use Arrays. When to use Objects. JavaScript does not support associative arrays. You should use objects when you want the element names to be strings (text). You should use arrays when you want the element names to be numbers.
With JavaScript, the full array can be accessed by referring to the array name: Arrays are a special type of objects. The typeof operator in JavaScript returns "object" for arrays. But, JavaScript arrays are best described as arrays. Arrays use numbers to access its "elements". In this example, person [0] returns John:
1)JavaScript arrays are zero-based indexed. In other words, the first element of an array starts at index 0, the second element starts at index 1, and so on. 2) How to Get an Array Size. So, let's start with that. Typically, all arrays have a length property which returns the number of elements or in other words, size of an array.
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#
public void CSharpFunction(dynamic length)
{
for (int i = 0; i < length; i++)
{
dynamic obj = webBrowser1.Document.InvokeScript("getObj", new object[] { i });
string val = obj.MyClassValue;
}
}
HTML / JavaScript
<script type="text/javascript">
var x = [];
x.push({MyClassValue: "Hello"});
x.push({MyClassValue: "People"});
function test() {
window.external.CSharpFunction(x.length);
}
function getObj(i) {
return x[i];
}
</script>
<button onclick="test();" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With