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


using webbrowser on form , calling c# javascript window.external. passing in javascript array of classes in function:

var x = []; x.push(classa); x.push(classa);  window.external.csharpfunction(x); 

i can x.length in c# with:

int length=(int)x.gettype().invokemember("length", bindingflags.getproperty, null, x, null); 

my question how x[0] , 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 fire off errors within webbrowser control.

thanks

please see question:

accessing properties of javascript objects using type dynamic in c# 4

i've tried retrieve values properties on objects in array without success. retrieving simple values in array, such strings , integers easy, have had no luck complex objects.

your best bet (imo) implement similar solution 1 in above question. instead of passing array directly, build javascript method can invoked c# return object in array @ specified index. so:

c#

public void csharpfunction(dynamic length) {     (int = 0; < length; i++)     {         dynamic obj = webbrowser1.document.invokescript("getobj", new object[] { });          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();" /> 

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -