Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Roslyn Scripting

Tags:

c#

roslyn

Hi I want to use Roslyn to scripting in my app. But I have new (September) version and I am confused. I want to execute file with some simply function. For example:

public int m(){
      return 6;
}

I found some articles about it for example acticle. There is some way to do it, but in my version isn't Session.Create() I want to use it like IronPython scripting

Something like:

var scriptEngine = new SciptEngine.ExecuteFile(fileWithFunction);
dynamic d = getFunction(m);

or

dynamic d = callFunction(m);

Is it possible or I must use IronPython scripting?

like image 260
wolen Avatar asked Sep 26 '12 13:09

wolen


1 Answers

The API hasn't changed significantly since the original CTP that the article was written in. For your scenario:

var scriptEngine = new ScriptEngine();
var session = scriptEngine.CreateSession();
session.AddReference("System");
session.AddReference("System.Core");
session.Execute(@"public int m() { return 6; }");

int six = session.Execute<int>("m()");
like image 185
Jason Malinowski Avatar answered Sep 30 '22 04:09

Jason Malinowski