I'm having some trouble passing JavaScript arrays and dictionary-type objects to methods in my managed code via the HTMLBridge. The infomation in Microsoft article detailing this topic and around the net have led me nowhere.
Using the following info from the linked article:
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:
JavaScript arrays are converted to
object[]
.JavaScript dictionaries are converted to
Dictionary<string,object>
.
... I've attempted to pass arrays and dictionary-objects to my managed code in several ways, with no 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>[]>();
}
Exceptions are of the 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)
(Analogous attempts were made to pass dictionary-objects to C# as Dictionary<string, object>
).
Are these attempts the result of misinterpretations of the info in the aforementioned article and beyond? Or is my implementation simply incorrect?
Addendum:
I'm aware of a way to accomplish what I desire with ScriptObject.getProperty()
, 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.