Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy aren't my JavaScript arrays able to be converted to the appropriate types?
    primarykey
    data
    text
    <p>I'm having some trouble passing JavaScript arrays and dictionary-type objects to methods in my managed code via the HTMLBridge. The infomation in <a href="http://msdn.microsoft.com/en-us/library/cc645079%28v=vs.95%29.aspx" rel="nofollow">Microsoft article detailing this topic</a> and around the net have led me nowhere.</p> <p>Using the following info from the linked article:</p> <blockquote> <p>NET Framework properties or input parameters typed as object are converted using the following heuristics when marshaling by value to a target .NET Framework property or input parameter:</p> <p><strong>JavaScript arrays</strong> are converted to <code>object[]</code>.</p> <p><strong>JavaScript dictionaries</strong> are converted to <code>Dictionary&lt;string,object&gt;</code>.</p> </blockquote> <p>... I've attempted to pass arrays and dictionary-objects to my managed code in several ways, with no success:</p> <p><strong>Javascript:</strong></p> <pre><code>var array = [{key: 1}, {key: 2}, {key: 3}]; silverlight_domElement.content.testObject.testMethod(array); </code></pre> <p><strong>C# (attempt #1):</strong></p> <pre><code>[ScriptableMember] //THROWS CONVERSION EXCEPTION HERE public void testMethod(Dictionary&lt;string,object&gt;[] arrayParam) { //... } </code></pre> <p><strong>C# (attempt #2):</strong></p> <pre><code>[ScriptableMember] public void testMethod(object arrayParam) { //THROWS CONVERSION EXCEPTION HERE Dictionary&lt;string, object&gt;[] arr = (Dictionary&lt;string, object&gt;[])arrayParam; } </code></pre> <p><strong>C# (attempt #3):</strong></p> <pre><code>[ScriptableMember] public void testMethod(ScriptObject arrayParam) { //THROWS CONVERSION EXCEPTION HERE Dictionary&lt;string, object&gt;[] arr = arrayParam.ConvertTo&lt;Dictionary&lt;string, object&gt;[]&gt;(); } </code></pre> <p><strong>Exceptions are of the form</strong> (where "TARGET TYPE" is the expected type of the object resulting from an explicit or implicit cast (including <code>Object[]</code>):</p> <blockquote> <p>SCRIPT16389: System.ArgumentException: This object cannot be converted to the specified type <strong>TARGET TYPE</strong>. Parameter name: targetType </p> <p>at System.Windows.Browser.ScriptObject.ConvertTo(Type targetType, Boolean allowSerialization) </p> <p>at System.Windows.Hosting.ScriptingInterface.GetScriptParamValueForType(ScriptParam scriptParam, Type desiredType) </p> <p>at System.Windows.Hosting.ScriptingInterface.ConvertFromScriptParams(ParameterInfo[] parameters, ScriptParam[] args) </p> <p>at System.Windows.Browser.ManagedObjectInfo.ScriptMethod.Invoke(ManagedObject obj, InvokeType invokeType, ScriptParam[] args) </p> <p>at System.Windows.Browser.ManagedObjectInfo.Invoke(ManagedObject obj, InvokeType invokeType, String memberName, ScriptParam[] args)</p> <p>at System.Windows.Hosting.ManagedHost.InvokeScriptableMember(IntPtr pHandle, Int32 nMemberID, Int32 nInvokeType, Int32 nArgCount, ScriptParam[] pArgs, ScriptParam&amp; pResult, ExceptionInfo&amp; pExcepInfo)</p> </blockquote> <p>(Analogous attempts were made to pass dictionary-objects to C# as <code>Dictionary&lt;string, object&gt;</code>).</p> <p>Are these attempts the result of misinterpretations of the info in the aforementioned article and beyond? Or is my implementation simply incorrect?</p> <p><strong>Addendum:</strong></p> <p><em>I'm aware of a way to accomplish what I desire with <a href="http://msdn.microsoft.com/en-us/library/system.windows.browser.scriptobject%28v=vs.95%29.aspx" rel="nofollow">ScriptObject</a><code>.getProperty()</code>, but I would like to deal with concrete, exact types if possible. Not to mention the fact it returns either a native type, String, or ScriptObject if the keyed value cannot be unboxed as either of the former two. I'd hate to be reduced to repeatedly calling it on an arbitrarily nested object until I arrive at a native type.</em></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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