Exposing .NET collection

Jan 1, 2015 at 5:06 AM
Edited Jan 1, 2015 at 5:07 AM
Is there a nice way to expose my .NET collection type without having to wrap the collection and items in an ObjectInstance? feels a bit painful to be doing this.

Any suggestions would be appreciated, I have not had any luck exposing a collection directly to my JS function and then being able to add a new item to it for example:
_engine.SetGlobalValue("Item", typeof(Item));
_engine.SetGlobalValue("Items", typeof (Items));

function test(items) {
    items.Add(new Item());
}

_engine.CallGlobalFunction("test", new Items());

Thanks
Stefan
Jan 1, 2015 at 8:45 PM
Another example, the follow will not work I end up with the following error "Cannot convert object of type 'UserQuery+Person' to an object.Parameter name: value", I can perfectly fine execute Console.WriteLine(item) though, just seems to not like property access.
void Main()
{
var engine = new ScriptEngine{EnableExposedClrTypes = true};

engine.SetGlobalValue("Console", typeof(Console));
engine.Execute("function foo(item) { Console.WriteLine(item.Name); }");
engine.CallGlobalFunction("foo", new Person());
} 

class Person {
public string Name {
    get { return "Stefan"; }
}
}
Jan 1, 2015 at 8:51 PM
Also interestingly the following works, but as mentioned in a previous post I do suffer a performance hit of executing it this way.
engine.SetGlobalValue("item", new Person());
engine.Execute("foo(item);");


Thanks
Jan 1, 2015 at 9:09 PM
Edited Jan 1, 2015 at 9:21 PM
Stupid me I found the ClrInstanceWrapper, all is well now, it seems that this does not support the JSFunction attribute to override the names, I might look into adding this.


Cheers