Skip to content

Instantly share code, notes, and snippets.

@istupakov
Created November 4, 2015 21:14
Show Gist options
  • Save istupakov/caf8e7d3d367c027f6e4 to your computer and use it in GitHub Desktop.
Save istupakov/caf8e7d3d367c027f6e4 to your computer and use it in GitHub Desktop.
Test Roslyn Scripting API. It's cool!
using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Scripting;
using Microsoft.CodeAnalysis.Scripting.CSharp;
using static System.Console;
namespace CSharpScriptingTest
{
class Program
{
static void Main(string[] args)
{
TestScripting().Wait();
}
static async Task TestScripting()
{
var options = ScriptOptions.Default.WithReferences("System.Core");
var expr = await CSharpScript.EvaluateAsync<Expression<Func<double, double>>>("t => t == 0 ? 34 : t + 1", options);
var f = expr.Compile();
WriteLine($"f = {expr.ToString()}");
WriteLine($"f(0) = {f(0)}");
WriteLine($"f(1) = {f(1)}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment