Skip to content

Add optional automatic conversion between .NET and JavaScript arrays #634

@CommonGuy

Description

@CommonGuy

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.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions