title: Getting JavaScript custom types returned to C# from InvokeScript with the custom type structure preserved
body: <p>I am using InvokeScript on a WebBrowser control to try and make a simple map using the Google Maps JavaScript API for HTML. I have a few functions written in the HTML page in JavaScript to do things with the map and I have duplicated those methods in my WPF application in C# and edited them so they call methods and do what they need to do to interface with the JavaScript.</p>
  
  <p>I am having a problem because not all of the values returned by the javascript for the map are simple, single value type like strings, integers or booleans and return custom types like LatLng and I don't know how to 'preserve' the returned value as separate 'parts' of the custom type. To help explain more:</p>
  
  <p>Normally I can use the following code:</p>
  
  <pre><code>return Convert.ToInt16(browser.InvokeScript("getHeading"));
  </code></pre>
  
  <p>Which calls the following function in JavaScript:</p>
  
  <pre><code>function GetHeading() {
      return map.getHeading();
  }
  </code></pre>
  
  <p>And returns a simple integer value for the current heading property of the map. This is easy and there is no problem with this. But some functions have a return type that is custom. </p>
  
  <p>The LatLng type is made up of a latitude value and a longitude value so it can't return a simple value and if I try it with the first code snippet of this post then it throws an error. For some functions like the following one:</p>
  
  <pre><code>function GetCentre() {
      return map.getCenter();
  }
  </code></pre>
  
  <p>Returns a LatLng containing the latitude and longitude of the centre of the map. How can I preserve the returned value and keep it as the latitude and longitude WHICH WILL THEN be put into a custom type in C# like this:</p>
  
  <pre><code>public class LatLng
  {
      public string latitude { get; set }
      public string longitude { get; set; }
  }
  </code></pre>
  
  <p>Do I have to write two separate functions on the javascript side?: One to return the latitude of the centre and another to return the longitude of it. Basically what I want to do is using InvokeScript on the WebBrowser control, call the javascript function that returns a custom type and keep the value in the custom type which I can then use in C# like returnedValue.latitude and returnedValue.longitude. How can I do this, thanks?</p>