Generic types

Apr 1, 2015 at 4:02 PM
In the front page of the project it say:
Full support for generic types and methods, including C#-like type inference and explicit type arguments
I am using VBScript...

What would be the proper syntax in vb to instatiate a .Net generic type?
Coordinator
Apr 1, 2015 at 5:40 PM
Hi,

Suppose you want to use .NET's Dictionary generic type. The first step is to expose the generic type itself:
engine.AddHostType("Dictionary", typeof(Dictionary<,>));
Next, expose any additional types you need to create the required specific dictionary type. For example:
engine.AddHostType("DayOfWeek", typeof(DayOfWeek));
engine.AddHostType("Int32", typeof(int));
Now you can instantiate your specific type from VBScript code:
engine.AddHostObject("host", new HostFunctions());
engine.Execute(@"
    dict = host.newObj(Dictionary(DayOfWeek, Int32))
    dict.Add DayOfWeek.Monday, 123
");
Note that it's easier and often sufficient to simply expose the specific type instead:
engine.AddHostType("StringDictionary", typeof(Dictionary<string, object>));
engine.Execute(@"
    dict = host.newObj(StringDictionary)
    dict.Add ""abc"", 123
");
Good luck!
Apr 1, 2015 at 9:55 PM
This is cool!!!

Where HostFunctions() is defined?
Coordinator
Apr 2, 2015 at 12:01 AM
The HostFunctions class is part of the ClearScript library. Take a look at the ClearScript Library Reference, available here.
Apr 2, 2015 at 1:52 PM
Awesome, this is working!
Sep 12, 2015 at 9:32 AM
Edited Sep 12, 2015 at 9:33 AM
Hi,

Is there is a way to use a generic type with a javascript class ?
For example, with the List<T> dotnet Framework class, If I define a javascript class "MyClass", is it possible to have a List<MyClass> ?

Here a pseudo code (that does not run) :
C# :
engine.AddHostObject("DotNet", new HostTypeCollection("mscorlib", "System.Core", "System.Data"));
Javascript
engine.Execute(@"
    function MyClass() {
        this.MyValue = 10;
    }
    var list = host.newObj(DotNet.System.Collections.Generic.List(MyClass));
");
Thanks for advance
Sybaris
Coordinator
Sep 12, 2015 at 5:37 PM
Edited Sep 12, 2015 at 9:00 PM
Hi Sybaris,

A JavaScript class (really just a function) cannot be used as a .NET type argument. As far as the managed side is concerned, it's just a script object and has no public type beyond DynamicObject.

Therefore an instance of a JavaScript class, like any script object, can be stored in a managed collection of type DynamicObject, IDynamicMetaObjectProvider, or simply Object:
function MyClass() {
    this.MyValue = 10;
}
var list = new DotNet.System.Collections.Generic.List(DotNet.System.Object);
list.Add(new MyClass());
var test = list[0].MyValue; // test == 10
Thanks for your question, and good luck!
Sep 13, 2015 at 7:45 PM
Hi,

I understand.
My goal is to allow script developers to define structs or classes that will be usable by C# classes.
I want to offer an API that is data independent, and the data structure have to be defined by script developers.

So another idea I have, is to offer another API to construct dynamicly a C# class.
Is it possible then to makes an AddHostType of a dynamic C# class ?

Thanks for advance

Sybaris
Coordinator
Sep 14, 2015 at 2:01 PM
Hi again,

My goal is to allow script developers to define structs or classes that will be usable by C# classes.

ClearScript allows C# code to access script objects directly via the dynamic keyword. Another possibility might be to use ClearScript's PropertyBag class, which gives you .NET collections that script code can treat much like native script objects.

So another idea I have, is to offer another API to construct dynamicly a C# class. Is it possible then to makes an AddHostType of a dynamic C# class ?

That's certainly an interesting way to go, and you should be able to use AddHostType() as long as you have a Type instance. We haven't tested with dynamically built types, but in theory they should work with ClearScript.

Good luck!