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

Type argument : how to make a typeof

Sep 10, 2015 at 11:27 AM
Edited Sep 10, 2015 at 11:29 AM
Hello,

I exposed DataTable type to my javascript code :
C# code :
scriptEngine.AddHostObject("DotNet", new HostTypeCollection("mscorlib", "System.Core", "System.Data"));
But for adding columns, the Add method require a Type Parameter.
My Javascript code :
var dt = new DotNet.System.Data.DataTable("testDataTable");
dt.Columns.Add("Col1",DotNet.System.Int32);
The last line does not run 'invalid argument.
In C#, I should write this :
dt.Columns.Add("Col1",typeof(System.Int32));
How can I do the same of typeof in ClearScript / Javascript ?

Thanks for advance
Sybaris
PS : I discover ClearScript for a few days, and I am very impressed by all the work made, and the power of this engine...
Coordinator
Sep 10, 2015 at 12:05 PM
Hi Sybaris,

ClearScript's HostFunctions class provides a number of utility methods for situations like this, including typeOf(). The only other thing to be aware of is that ClearScript blocks reflection by default, but you can easily enable it:
scriptEngine.AddHostObject("host", new HostFunctions());
scriptEngine.AllowReflection = true;
scriptEngine.Execute(@"
    var dt = new DotNet.System.Data.DataTable('testDataTable');
    dt.Columns.Add('Col1', host.typeOf(DotNet.System.Int32));
");
Cheers!
Marked as answer by sybarisfr on 3/30/2016 at 10:57 PM
Sep 10, 2015 at 12:11 PM
Perfect :-)

Thanks a lot