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

New Post: V8 Iterators

$
0
0
Hi again,

Amazing. Works perfectly.

Well, not quite :) It doesn't support strongly-typed enumeration via IEnumerable<T>. We'll add more robust support for ES6 iteration in a future update.

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?

Sure. Here's a simple extension class that implements a script-friendly forEach method for .NET enumerables:
publicstaticclass ScriptEnumerable {
    publicstaticvoid forEach<T>(this IEnumerable<T> source, object action) {
        source.ToList().ForEach(item => ((dynamic)action)(item));
    }
}
And here's some test code that demonstrates its usage:
engine.Script.testDictionary = new Dictionary<string, object> {
    { "foo", 123 },
    { "bar", 456.789 },
    { "baz", "hello" },
    { "qux", DayOfWeek.Wednesday }
};
engine.AddHostType(typeof(ScriptEnumerable));
engine.AddHostType(typeof(Console));
engine.Execute(@"
    testDictionary.forEach(function(item) {
        Console.WriteLine(item);
    })
");
Cheers!

Viewing all articles
Browse latest Browse all 2297

Trending Articles



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