Is there a good way to extend collection type conversion logic? #507
-
As far as I understand, JavaScript arrays extend IList, and IList which means they can only be used when calling methods whose argument are such collections. However, usually in C# we define APIs which take collections with stricter types, e.g. IEnumerable. Calling these functions requires interacting with the ClearScript runtime to obtain an appropriately typed collection. Is there a way to inject this logic without leaking ClearScript into the script, and without having to add IEnumerable overloads and similar to each API where this occurs. It would be nice if we could define type mapping logic on the C# side. So if ClearScript cannot resolve a method with the appropriate parameter types then it tries to lookup a conversion function which makes the types match. That way a script author can write In this case we would define a mapping from |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Hi @handerss-tibco,
There's almost certainly a way to set up a JavaScript API so that scripts needn't directly invoke ClearScript services. However, since ClearScript provides no simple hook for argument conversion, there's probably no avoiding per-method setup of some sort. Overloads are one possibility, as are extension methods, the facade pattern, etc. If you can say more about your scenario, we'll be happy to provide more specific recommendations. Thanks! |
Beta Was this translation helpful? Give feedback.
Unfortunately, no.
Method binding in .NET is a rather complicated affair – so much so that ClearScript makes no attempt to duplicate the algorithm. Instead, it delegates the task to portions of the C# compiler that are accessible at runtime. It's the compiler that throws that exception.
To put it another way, ClearScript provides a call signature that the C# compiler binds to a method – one that it may have selected from a list of overloads according to a complex ruleset, constructed from a generic method definition, etc.
Because we don't have the method when we're building the call signature, we have no …