title: Accessing objects returned by java script running in a webbrowser control from C#
body: <p>I would like to know how i can access properties of an object returned by a javascript function running inside a webbrowser control from within C#. </p>
  
  <p>Here is the full context:</p>
  
  <p>I'd like to create a C# mapping control based on OpenStreetMap's open layers javascript library. The OSM Map is displayed in a webpage shown inside the webbrowser element. I'm using InvokeScript in order to control the map by calling a simplified java script wrapper for the open layers library. Everything is working fine.</p>
  
  <p>In the C# wrapper I'd like to implement a function returning the center coordinates of the Map currently beeing on display in the webbrowser control. So far i came only up with a pretty unelegant solution:</p>
  
  <p>In my C# wrapper i have two functions that i need both in order to query the map center. First i have a function called GetCenter which uses InvokeScript to call a JavaScript function which in turn calls another C# function (SetCenterFromJScript) to set the map center in my C# wrapper:</p>
  
  <pre><code>public Coord GetCenter()
  {
    object center = webBrowser.Document.InvokeScript("set_center_in_wrapper");
  
    // I'm stuck here:
    Type type = center.GetType();
    PropertyInfo[] pi = type.GetProperties();
  
    // I already get the center as an object, but how can i access the
    // properties "lon" and "lat" directly so that i can get rid of m_center 
    // and SetCenterFromJScript alltogether?
  
    return m_center;
  }
  
  public void SetCenterFromJScript(double lon, double lat)
  {
    m_center.lon = lon;
    m_center.lat = lat;
  }
  </code></pre>
  
  <p>The Java script function is calling C# via the ObjectForScripting mechanism as shown here:</p>
  
  <pre><code>function set_center_in_wrapper()
  {
    var center = map.getCenter().transform(new OpenLayers.Projection("EPSG:900913"), 
                                           new OpenLayers.Projection("EPSG:4326"))
  
    window.external.SetCenterFromJScript(center.lon, center.lat);
    return center // &lt;- useless at the moment since i don't know how to access the data from C#
  }
  </code></pre>
  
  <p>At the moment the return value is useless, i'm using SetCenterFromJScript to submit the coordinates. How can i access the Properties "lon" and "lat" of center from C# directly?</p>
  
  <p>Thanks in advance, any help is appreciated.</p>