Roslyn: [Scripting] Revisit Invoke, CreateDelegate methods on ScriptState

Created on 28 Jun 2015  路  6Comments  路  Source: dotnet/roslyn

Revisit commented out public methods.

Area-Interactive Interactive-ScriptingLogic

Most helpful comment

You can work around the lack of CreateDelegate by creating delegates directly in the script:

C# var state = await CSharpScript.RunAsync("var times = new System.Func<int, int>(x => x * x);"); var fn = (Func<int, int>)state.GetVariable("times").Value; var result = fn(5);

All 6 comments

I would much much appreciate this being brought back even with the imperfect overload resolution.

Hi, Looks like CreateDelegate method is still not available in the latest version ( >= 2.4)?
I am looking to invoke a method described inside a script by the host application.
Any other way to achieve this?

This is very much similar to your existing test:

public void TestCreateMethodDelegate()
{
// create a delegate to a method declared in the script
var state = CSharpScript.Run("int Times(int x) { return x * x; }");
var fn = state.CreateDelegate>("Times");
var result = fn(5);
Assert.Equal(25, result);
}

@ShekharGupta1988 Do you have a script that defines multiple methods and the host needs to chose some of them?

Hi @tmat - Yes. The script file would have multiple methods and host needs to execute one of those methods based on certain conditions.

You can work around the lack of CreateDelegate by creating delegates directly in the script:

C# var state = await CSharpScript.RunAsync("var times = new System.Func<int, int>(x => x * x);"); var fn = (Func<int, int>)state.GetVariable("times").Value; var result = fn(5);

@tmat - thanks for the work around :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alrz picture alrz  路  125Comments

MadsTorgersen picture MadsTorgersen  路  249Comments

MadsTorgersen picture MadsTorgersen  路  120Comments

MadsTorgersen picture MadsTorgersen  路  170Comments

stephentoub picture stephentoub  路  167Comments