Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: Question about passing generic List.


Here's what's going on. In your script code, the expression Me.MyList is of the managed type IList<Item>. This type has an indexer that is actually an indexed property named "Item". ClearScript supports indexed properties, but it requires the following syntax:
// JavaScriptvar value = Me.MyList.Item.get(index); // or Me.MyList.Item(index)
Me.MyList.Item.set(index, value);
When you change the type to List<Item>, you enable ClearScript's special treatment of objects that implement IList. This feature supports array-like access from script code:
// JavaScriptvar value = Me.MyList[index];
Me.MyList[index] = value;
Note that IList<T> is not derived from IList and therefore doesn't trigger this feature.

It looks like that engine.Execute() uses the static declaration type instead of the actual type of the object Me.MyList. Is this a defect ?

No, this is by design. ClearScript's goal is to provide C#-like access to .NET types. .NET supports things like generic and overloaded methods, and C# method binding relies on static analysis of the method arguments. Script languages don't support static typing, so ClearScript simulates it with a feature called type restriction, which allows C#-like method binding to take place at runtime.

Is there a way to configure ClearScript to fix this problem?

You can use ClearScript's ScriptMemberAttribute to expose the runtime type of a given member:
public IList<Item> MyList = new List<Item> { new Item(), new Item(), new Item() };
You can also use the engine's DisableTypeRestriction property to turn off type restriction globally, but doing so can break some method invocation scenarios.

Good luck!

Viewing all articles
Browse latest Browse all 2297