Skip to content

Instantly share code, notes, and snippets.

@jonathascosta
Created January 30, 2013 08:35
Show Gist options
  • Save jonathascosta/4671673 to your computer and use it in GitHub Desktop.
Save jonathascosta/4671673 to your computer and use it in GitHub Desktop.
Testing Roslyn's ScriptEngine
var engine = new ScriptEngine();
new[] {
"System",
"System.Core"
}.ToList().ForEach(assembly => engine.AddReference(assembly));
new[] {
"System",
"System.Linq"
}.ToList().ForEach(assembly => engine.ImportNamespace(assembly));
var session = engine.CreateSession(this);
session.Execute("var P1 = 9.8m;");
session.Execute("var P2 = 6.7m;");
session.Execute("var P3 = 8m;");
session.Execute("var P4 = 3m;");
session.Execute("decimal Min(params decimal[] args) { return args.Min(); }");
session.Execute("bool Check5(params decimal[] args) { return args.Min() >= 5; }");
var A = session.Execute("(P1 + P2 + P3) / 3");
var B = session.Execute("Check5(P1, P2, P3)? (P1 + P2 + P3) / 3 : (P1 + P2 + P3 - Min(P1, P2, P3) + P4) / 3");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment