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

New Post: Func and delegate

$
0
0
Hello Sybaris,

If you expose an open generic type, script code can invoke the exposed object to yield a closed generic type:
engine.AddHostObject("DotNet", new HostTypeCollection("mscorlib", "System.Core"));
engine.AddHostType("SomeFunc", typeof(Func<,>));
engine.Execute(@"
    var System = DotNet.System;
    var IntToStringFunc = SomeFunc(System.Int32, System.String);
    var f = new IntToStringFunc(function(n) {
        return 'The value is ' + n + '.';
    });
");
Also, a single JavaScript new expression can close a generic type and pass arguments to its constructor:
engine.AddHostObject("DotNet", new HostTypeCollection("mscorlib", "System.Core"));
engine.AddHostType("SomeFunc", typeof(Func<,>));
engine.Execute(@"
    var System = DotNet.System;
    var f = new SomeFunc(System.Int32, System.String, function(n) {
        return 'The value is ' + n + '.';
    });
");
This syntax seems to be what you're looking for in your first question.

As for your second question, host type collections merge overloaded types when they differ only in their generic parameter count, so you can already do things like this:
engine.AddHostObject("DotNet", new HostTypeCollection("mscorlib", "System.Core"));
engine.Execute(@"
    var System = DotNet.System;
    var f1 = new System.Func(System.Int32, System.String, function(n) {
        return 'The value is ' + n + '.';
    });
    var f2 = new System.Func(System.Double, System.Double, System.Double, function(x, y) {
        return x * y;
    });
");
Please let us know if you have more questions.

Thanks!

Viewing all articles
Browse latest Browse all 2297

Trending Articles



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