I have encountered difficulties passing JavaScript arrays and dictionary-like objects to methods in my managed code through the HTMLBridge. Despite searching for information on this topic in a Microsoft article and various other sources, I have not found a solution.
Referring to the information provided in the linked article:
.NET Framework properties or input parameters that are typed as object undergo certain conversions when marshaled by value to a target .NET Framework property or input parameter:
JavaScript arrays are converted to
object[]
.JavaScript dictionaries are converted to
Dictionary<string,object>
.
... I have made several attempts to pass arrays and dictionary-like objects to my managed code without success:
Javascript:
var array = [{key: 1}, {key: 2}, {key: 3}];
silverlight_domElement.content.testObject.testMethod(array);
C# (attempt #1):
[ScriptableMember]
//Throws conversion exception here
public void testMethod(Dictionary<string,object>[] arrayParam)
{
//...
}
C# (attempt #2):
[ScriptableMember]
public void testMethod(object arrayParam)
{
//Throws conversion exception here
Dictionary<string, object>[] arr = (Dictionary<string, object>[])arrayParam;
}
C# (attempt #3):
[ScriptableMember]
public void testMethod(ScriptObject arrayParam)
{
//Throws conversion exception here
Dictionary<string, object>[] arr =
arrayParam.ConvertTo<Dictionary<string, object>[]>();
}
The exceptions appear in the following form (where "TARGET TYPE" is the expected type of the object resulting from an explicit or implicit cast, including Object[]
):
SCRIPT16389: System.ArgumentException: This object cannot be converted to the specified type TARGET TYPE. Parameter name: targetType
at System.Windows.Browser.ScriptObject.ConvertTo(Type targetType, Boolean allowSerialization)
at System.Windows.Hosting.ScriptingInterface.GetScriptParamValueForType(ScriptParam scriptParam, Type desiredType)
at System.Windows.Hosting.ScriptingInterface.ConvertFromScriptParams(ParameterInfo[] parameters, ScriptParam[] args)
at System.Windows.Browser.ManagedObjectInfo.ScriptMethod.Invoke(ManagedObject obj, InvokeType invokeType, ScriptParam[] args)
at System.Windows.Browser.ManagedObjectInfo.Invoke(ManagedObject obj, InvokeType invokeType, String memberName, ScriptParam[] args)
at System.Windows.Hosting.ManagedHost.InvokeScriptableMember(IntPtr pHandle, Int32 nMemberID, Int32 nInvokeType, Int32 nArgCount, ScriptParam[] pArgs, ScriptParam& pResult, ExceptionInfo& pExcepInfo)
(Similar attempts were made to pass dictionary-like objects to C# as Dictionary<string, object>
).
Are these unsuccessful attempts due to misinterpretation of the information in the referenced article and beyond? Or is there a flaw in my implementation?
Addendum:
I am aware of using ScriptObject.getProperty()
to achieve what I need, but I prefer working with concrete, specific types if possible. Moreover, it returns either a native type, String, or ScriptObject if the keyed value cannot be unboxed as either of the former two. I would rather not resort to repeatedly calling it on deeply nested objects until reaching a native type.