Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access JavaScript array elements from C# (via WebBrowser)?

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

like image 954
Jamie Mueller Avatar asked Jun 05 '12 19:06

Jamie Mueller


People also ask

How do you access the values within an array in C?

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. )

How do you access an array element in JavaScript?

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.

How do you access the elements of an array?

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.

How do you access an array inside an array?

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.

How to access array elements in JavaScript?

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 should I use arrays and objects in JavaScript?

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.

How to access the full array in JavaScript?

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:

How to get an array size in JavaScript?

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.


1 Answers

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();" />
like image 86
Joshua Avatar answered Oct 23 '22 15:10

Joshua