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

New Post: V8 Iterators

$
0
0
Amazing. Works perfectly. Thank you.

May I ask another -- related -- question? The goal is Linq-style iterating, e.g. implementing a forEach with a callback parameter.

Here http://www.csharpcity.com/2013/from-c-to-javascript-and-back-again-via-clearscript/ it is explained how to pass a JS callback to a .NET function requiring an Action parameter (some code added by myself and tested, it works):
C#:

public class MyNetCollection {
   public void forEach (Action<object> a) { ...}
}
...
engine.AddHostType("Action", typeof(Action<object>));
...

JS:

// we want to sum up all values in myNetCollection
var s = 0;
myNetCollection.forEach ( new Action( function(v) {
   s += v; // &#43; should be a plus
} ));
Now that wrapping of the callback function into the new Action ( ) constructor is inconvenient. Is there some clean way of doing that on the .NET side? I.e. such that forEach accepts a parameter of type object and does the wrapping into the Action itself? I think I could abuse some method of the HostFunctions class and create a delegate, yet the methods of the class do not seem to be intended to be used from outside a script? Is there a cleaner way? Assumption: myNetCollection, of course, knows which script engine it belongs to.

Viewing all articles
Browse latest Browse all 2297

Trending Articles



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