This project has moved. For the latest updates, please go here.

Can I pass an associative array/dictionary into the engine?

Jan 21, 2015 at 11:44 PM
Edited Jan 22, 2015 at 10:54 AM
I'd like to pass in a dictionary/associative array with keys that are variable. Something like.
// C# code...
// -------------
var data = new Dictionary<string, string>(); // or Hashtable() or NameValueCollection()
data.Add("name", "Bob");

engine.AddGlobalValue("person", data");
Then, in javascript, I'd like to do this:
// Javascript code...
// -----------
console.log(person.name);
//or
console.log(person['name'];
I could make a custom class with a property of "Name," but I won't know the keys at compile time, so I can't make a custom class for everything. How do I pass a simple Javascript object or associative array?
Coordinator
Jan 24, 2015 at 6:55 AM
How about this?
var data = engine.Object.Construct();
data["name"] = "Bob";

engine.AddGlobalValue("person", data);
If you need to use a Dictionary, it should be pretty easy to write a conversion method to convert to ObjectInstance.
Marked as answer by deanebarker on 1/26/2015 at 2:54 PM
Jan 26, 2015 at 10:54 PM
That works nicely, thank you.