This project has moved and is read-only. For the latest updates, please go here.

Func and delegate

Mar 30, 2016 at 9:10 PM
Hi,

I saw that the following code works :

C# :
public class ScriptAPI
{
       public MyMethod(Func<string, object> aFunc)
       {
              MessageBox.Show(aFunc("123").ToString())
       }
}

scriptEngine.AddHostObject("DotNet", new HostTypeCollection ("mscorlib", "System.Core", "System.Data"));
scriptEngine.AddHostObject("host", new HostFunctions());
scriptEngine.AddRestrictedHostObject("ScriptAPI", HostItemFlags.GlobalMembers, new ScriptAPI());
scriptEngine.AddHostType("SomeDelegate", typeof(Func<string, object>));
Javascript :
var f = new SomeDelegate(function (x) { return x; });
MyMethod(f);
I would go further, but I lack the syntax.


1°/ Question :
Instead of creating a delegate (like in my example "SomeDelegate") with preset type for the generic, I would like to do something like that :

C# :
  scriptEngine.AddHostType("SomeDelegate", typeof(Func<,>));
Javascript :
var f = new SomeDelegate(host.typeOf(DotNet.System.String), host.typeOf(DotNet.System.Object) (function (x) { return x; });
My last javascript line of code is not correct, and I hope you have understood what i search to do.
Could you give me please the correct syntax please ?

2°/ Question :
If one goes even further, is there a more elegant way to do than to declare as the following ? :

C# :
scriptEngine.AddHostType("Func1", typeof(Func<>));
scriptEngine.AddHostType("Func2", typeof(Func<,>));
scriptEngine.AddHostType("Func3", typeof(Func<,,>));
scriptEngine.AddHostType("Func4", typeof(Func<,,,>));
Thanks for advance for your answers.

Sybaris
Mar 30, 2016 at 10:23 PM
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!
Marked as answer by sybarisfr on 3/30/2016 at 10:56 PM
Mar 31, 2016 at 6:55 AM
Hello,

Wondeful, exactly the answer I was looking for :-)
ClearScript is a very great projet !!!

Thanks.
Sybaris
Mar 31, 2016 at 1:42 PM
Thank you very much!