Search code examples
c#.netjint

Reading a value from a List<Employee> in Jint


A general question first: Is there somewhere a documentation about jint? I was not able to find anything apart from the examples on the github page.

I'm trying to do something like the sample below in Jint. However I'm having troubles to read the value back.

var emp = new Employee {Name = "Mike"};

var employees = new List<Employee>();
employees.Add(emp);

var engine = new Engine();
engine.SetValue("employees", e);

// how to get the value "Mike" out of the engine?
JsValue name = engine.GetValue("e[0].Name");

Solution

  • I didn't get exactly what you are trying to do but based on jint's capabilities. you can use your employees list in JavaScript.

            var emp = new Employee { Name = "Mike" };
    
            var employees = new List<Employee>();
            employees.Add(emp);
    
            var engine = new Engine();
            engine.SetValue("employees", employees);
    
            // how to get the value "Mike" out of the engine?
            JsValue name = engine.Execute("employees[0].Name").GetCompletionValue();
            System.Console.WriteLine(name.ToString());//This should return Mike
    

    Hope this helps you.