Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

Commented Unassigned: Exporting array of Number from V8. [53]

$
0
0
Hi again, guys.

There is some way to export a array of Number ([1,2]) from JS evaluation to C# array of integer?

The JS code is:
```
(function (data){ return new RuleExecutionResult(data.BodyFat > 2 && data.Weight > 85, new CLRArray(1,2)) })(data)
```

RuleExecutionResult constructor is:

```
public WorkoutTemplateAssociatedData(bool isValid, int[] associatedObjectiveIds)
```

But ClearScript can't find it because "associatedObjectiveIds" is being binded to a dynamic (as you can see in the attached file), while isValid is correcly binded as true.



Comments: Hello! This behavior is by design. ClearScript doesn't convert arrays (or most other types) automatically. In general it favors full host and script object access over data conversion. Because host and script objects (even simple collections such as arrays) behave very differently, automatic conversion can be ambiguous and problematic. Full access allows hosts to provide their own conversions that fit their needs. Here's an example of a host-provided array conversion function: ``` C# // C# engine.Script.toIntArray = new Func<dynamic, int[]>(scriptArray => { var length = scriptArray.length; var intArray = new int[length]; for (var i = 0; i < length; i++) intArray[i] = scriptArray[i]; return intArray; }); ``` And here's how you might use it from script code: ``` JavaScript // JavaScript someHostObject.ProcessData(toIntArray([9,8,7,6,5])); ``` Please send any further questions or concerns. Good luck!

Viewing all articles
Browse latest Browse all 2297

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>