title: Accessing properties of javascript objects using type dynamic in C# 4
body: <p>I define a com object as a dynamic type in c# I am able to call methods quite easily.
  However when I try to access a property on the same object I get an invalid cast exception.</p>
  
  <p>The object in question is an array, passed to managed code from JavaScript, and I wish to get the length property of it as an int.</p>
  
  <p>I know I am missing something odd because I am not getting a 'does not contain a definition' exception and I can  access the property easily using reflection/InvokeMember.</p>
  
  <p>Why can I not convert the length property of the dynamic type to an int?</p>
  
  <p>For example</p>
  
  <p><strong>This Fails</strong></p>
  
  <pre><code>   dynamic com = comObject;
     int i = com.length; // RTBE here.
  </code></pre>
  
  <p><strong>This Works</strong></p>
  
  <pre><code>   Type type = comObject.GetType();
     int i = (int)type.InvokeMember("length", BindingFlags.GetProperty, null, comObject, null);
  </code></pre>
  
  <p><strong>* Update *</strong></p>
  
  <p>After a lot of testing I have narrowed this oddness to cases of multi-dimensional arrays.
  The com object in question is a parameter passed from a html document to managed code. For all intents and purposes the object sometimes looks like this in JavaScript.</p>
  
  <pre><code>var x = ["a1", "a2", "a3"];
  </code></pre>
  
  <p>When an array like this comes to managed code I am able to get the length AOK using the type dynamic. (i.e. the first example here that fails actually works). However, if it is a multi-dimensional array such as the following structure in JavaScript.</p>
  
  <pre><code>var y = [["b1", "b2", "b3"], "a2", "a3"];
  </code></pre>
  
  <p>Then I get an error when trying to access its length property dynamically. Note, I can still access the length via reflection in this case. It seems to me that for some reason the length property does not get correctly mapped when a multidimensional array is used as a dynmaic type...</p>
  
  <p>In my case what I have done to solve(!?) this is add a 'length_' property to the array like so before passing it.</p>
  
  <pre><code>var y = [["b1", "b2", "b3"], "a2", "a3"];
  y.length_ = y.length;
  </code></pre>
  
  <p>Now in managed code I can accesses this property as expected without error. Far from ideal but seems to work...</p>
  
  <pre><code>   dynamic com = comObject;
     int i = com.length_; // 3!
  </code></pre>
  
  <p><strong>Further Update</strong></p>
  
  <p>Ok, so it seems that as well as the length property the objects index gets lost to the dynamic type as well. Again it is accessible via reflection though...</p>
  
  <p><strong>Fails</strong></p>
  
  <pre><code>   dynamic com = comObject; // js array i.e. var x = [1, 2];
     int i = com[0]; // MissingMemberException - Error while invoking [PROPERTYGET, DISPID(0)].
     int i = com["0"]; // MissingMemberException - Error while invoking [PROPERTYGET, DISPID(0)].
  </code></pre>
  
  <p><strong>Works</strong></p>
  
  <pre><code>   Type type = comObject.GetType();
     int i = (int)type.InvokeMember("0", BindingFlags.GetProperty, null, comObject, null); // 1
  </code></pre>