Hi again,
Well, not quite :) It doesn't support strongly-typed enumeration via
Sure. Here's a simple extension class that implements a script-friendly
And here's some test code that demonstrates its usage:
Cheers!
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 thenew 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)); } }
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); }) ");