-
Notifications
You must be signed in to change notification settings - Fork 156
Description
It would be very helpful to have an optional automatic conversion between .NET and JavaScript arrays (similar to DateTime/Date and Task/Promise conversions).
Some conversation and a workaround are described in #228.
My main motivation is exposing an external .NET library (for example https://github.com/bchavez/Bogus with lots of methods) via ClearScript as a "clean" JavaScript interface. This works pretty well, with the exception of arrays. Since arrays are exposed to JavaScript as their underlying .NET type, certain JavaScript functions (such as Array.isArray
or Array.prototype.find()
) do not work.
One workaround would be to completely wrap the external .NET library in a custom .NET wrapper to convert all arrays to JavaScript arrays before returning them, but that is a lot of work (especially with nested objects, one would need to create new classes for each of them).
Another workaround is to return the .NET array as is, but then creating and calling a toJsArray()
method from the JavaScript side. The main drawback here is that one needs to call this method everywhere and that the JavaScript "client" needs to know about it. When trying to expose a "clean" JavaScript API to users, this leads to weird cases where you need to tell users to call toJsArray()
on all arrays before using them.