Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: Generic types

$
0
0
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!

Viewing all articles
Browse latest Browse all 2297

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>