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

New Post: Func and delegate

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

Viewing all articles
Browse latest Browse all 2297