Hi Klator, and thanks for your question!
There are several ways to do what you need, and JSON serialization should not be necessary. One thing you could do is access a .NET array directly from JavaScript:
There are several ways to do what you need, and JSON serialization should not be necessary. One thing you could do is access a .NET array directly from JavaScript:
public class Foo {
public string[] GetNames() {
return new[] { "Adolph", "Blaine", "Charles", "David" };
}
}
and then:engine.AddHostObject("foo", new Foo());
engine.AddHostType("Console", typeof(Console));
engine.Execute(@"
names = foo.GetNames();
for (var i = 0; i < names.Length; i++) {
Console.WriteLine(names[i]);
}
");
On the other hand, if it's important that the method's return value be a JavaScript array, ClearScript lets you do that too:public class Foo {
private readonly ScriptEngine engine;
public Foo(ScriptEngine engine) {
this.engine = engine;
}
public object GetNames() {
dynamic jsArray = engine.Evaluate("[]");
jsArray.push("Adolph");
jsArray.push("Blaine");
jsArray.push("Charles");
jsArray.push("David");
return jsArray;
}
}
and then:engine.AddHostObject("foo", new Foo(engine));
engine.AddHostType("Console", typeof(Console));
engine.Execute(@"
names = foo.GetNames();
for (var i in names) {
Console.WriteLine(names[i]);
}
");
Please let us know if these suggestions don't work for you! Good luck!