Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In simple terms you can't access the length property of a multi-dimensional array in c# via the type dynamic unless, it seems, you have used the length property in JavaScript first...</p> <p>The simple test below shows this very clearly. I hope this saves someone else the head scratching I have been having over the last day or so.</p> <pre><code>[ComVisibleAttribute(true)] public partial class Form1 : Form { public Form1() { InitializeComponent(); webBrowser1.ObjectForScripting = this; StringBuilder html = new StringBuilder(); html.Append("&lt;script&gt;"); html.Append("var arr1 = [1, 2, 3, 4];"); html.Append("var arr2 = [arr1, 2, 3, 4];"); html.Append("var fn1 = function() { return arr1; };"); html.Append("var fn2 = function() { return arr2; };"); html.Append("var fn3 = function() { alert(arr2.length); }"); html.Append("&lt;/script&gt;"); webBrowser1.DocumentText = html.ToString(); webBrowser1.DocumentCompleted += (o, e) =&gt; { dynamic arr1 = webBrowser1.Document.InvokeScript("fn1"); int i = arr1.length; MessageBox.Show(i.ToString()); //4 // If I call fn3 here then the arr2.length *is* available as int i2 below! ////webBrowser1.Document.InvokeScript("fn3"); // 4 dynamic arr2 = webBrowser1.Document.InvokeScript("fn2"); int i2 = arr2.length; MessageBox.Show(i2.ToString()); // unless fn3 is called you get... /* System.MissingMemberException was unhandled by user code Message=Error while invoking length. Source=System.Dynamic StackTrace: at System.Dynamic.ComRuntimeHelpers.CheckThrowException(Int32 hresult, ExcepInfo&amp; excepInfo, UInt32 argErr, String message) at CallSite.Target(Closure , CallSite , ComObject ) at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0) at CallSite.Target(Closure , CallSite , Object ) at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0) */ }; } } </code></pre> <p><strong>update</strong></p> <p>It seems (see comments) that this behaviour is fixed if the WebBrowser control uses version 9 of Internet Explorer (...the control uses the version of IE on the machine). I can only presume that the IE9 'Chakra' JavaScript engine is doing something extra/different to the old js engine in this case.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload