0

I have JS code:

function on_save() {
    Service.AddTableRow([{id: 21194, value: "jjkk"}, {id: 1234, value: "Lala"}]);
    return true;
}

Into c# I do:

var engine = new V8ScriptEngine();
engine.AddHostObject("Service", scriptService);
engine.Execute(content);
result = engine.Script.on_save();

Into scriptService I have:

public void AddTableRow(Dictionary<string, object> values)
{
    //but there is invalid argument "values"
    //I also tried List<object> param type but result is the same
}

How can I resolve this issue?

A. Gladkiy
  • 3,134
  • 5
  • 38
  • 82

1 Answers1

0

Try something like this:

public void AddTableRow(IList list) {
    foreach (ScriptObject item in list) {
        Console.WriteLine("{0} -> {1}", item["id"], item["value"]);
    }
}

You can see this sample working here.

EDIT: If you're using something older than ClearScript 6, you can do something like this instead:

public void AddTableRow(dynamic list) {
    int length = list.length;
    for (var i = 0; i < length; ++i) {
        var item = list[i];
        Console.WriteLine("{0} → {1}", item.id, item.value);
    }
}

This technique is also demonstrated here. Newer ClearScript versions support both.

BitCortex
  • 3,328
  • 1
  • 15
  • 19
  • No, error: `Error: The best overloaded method match for ScriptService.AddTableRow(System.Collections.IList)' has some invalid arguments` – A. Gladkiy Oct 05 '22 at 07:07
  • What are you passing to AddTableRow? My sample works with the JS above (in on_save). Also, what’s your ClearScript version? – BitCortex Oct 05 '22 at 10:46
  • I've added a link to a working .NET Fiddle in my answer. – BitCortex Oct 05 '22 at 12:58
  • I'm using ClearScript v5.4.1.0, and have error `Error CS0246 The type or namespace name 'ScriptObject' could not be found (are you missing a using directive or an assembly reference?)` – A. Gladkiy Oct 06 '22 at 10:26
  • Whoa, that's quite old; 5.5.3 is the first version available on NuGet. Anyway, see my updated answer above. – BitCortex Oct 06 '22 at 13:09
  • `'ScriptObject'` from what namespace? – A. Gladkiy Oct 06 '22 at 15:44
  • https://microsoft.github.io/ClearScript/Reference/html/T_Microsoft_ClearScript_ScriptObject.htm – BitCortex Oct 06 '22 at 15:56