To compile C# scripts these dll files are required:

  • Microsoft.CodeAnalysis
  • Microsoft.CodeAnalysis.CSharp
  • Microsoft.CodeAnalysis.Scripting
  • Microsoft.CodeAnalysis.Scripting.CSharp

All of then can be acquired by compiling Roslyn, instruction how to do that can be found here. For simplicity's sake I included precompiled assemblies in source code which can be found at the bottom.

 

Simple expression evaluation

var result = CSharpScript.Eval(@"25 + 30", ScriptOptions.Default);
Console.WriteLine("Eval: " + result);

 

Expression with global parameters

var result = CSharpScript.Eval(@"X + Y", ScriptOptions.Default, new GlobalParams { X = 12, Y = 25 });
Console.WriteLine("Eval: " + result);

 

Created scripts can be run several times with different parameters

var script = CSharpScript.Create(@"var result = X + Y;", ScriptOptions.Default);
var state = script.Run(new GlobalParams { X = 10, Y = 2 });
var state2 = script.Run(new GlobalParams { X = 1, Y = 3 });
Console.WriteLine("State variable: " + state.Variables["result"].Value);
Console.WriteLine("State2 variable: " + state2.Variables["result"].Value);

 

Script running environment can be configured with ScriptOptions class

var scriptOptions = ScriptOptions.Default.AddNamespaces("System.IO");
var script = CSharpScript.Create(@"var result = Path.Combine(""folder"", ""file"");", scriptOptions);
var state = script.Run();
Console.WriteLine("State variable: " + state.Variables["result"].Value);

 

Scripts can also be converted to functions

var script = CSharpScript.Create(@"int Sum(int a, int b) { return a + b;}", ScriptOptions.Default);
var state = script.Run();
var sumFunction = state.CreateDelegate<Func<int, int, int>>("Sum");
Console.WriteLine("Sum function: " + sumFunction(2,22));

 

Source files with required binaries included.

 

UPDATE 2015-09-22:

As mentioned in comments by coder there was one missing assembly System.Collections.Immutable which is not required in project but is used by one of previously listed assemblies. Don’t have required version of System.Collections.Immutable.dll, so recompiled all assemblies and update existing example. Few notable changes, all methods now are asynchronous and in delegate creation there is no way to select specific function, at least I didn’t find a way.

 

public static async void EvalWithoutParameters()
{
    var result = await CSharpScript.EvaluateAsync(@"25 + 30", ScriptOptions.Default);
    Console.WriteLine("Eval: " + result);
}

public static async void EvalExpression()
{
    var result = await CSharpScript.EvaluateAsync(@"X + Y", ScriptOptions.Default, new GlobalParams { X = 12, Y = 25 });
    Console.WriteLine("Eval: " + result);
}

public static async void ScriptCreateAndRun()
{
    var script = CSharpScript.Create(@"var result = X + Y;", ScriptOptions.Default, typeof(GlobalParams));
    var state = await script.RunAsync(new GlobalParams { X = 10, Y = 2 });
    var state2 = await script.RunAsync(new GlobalParams { X = 1, Y = 3 });
    Console.WriteLine("State variable: " + state.Variables["result"].Value);
    Console.WriteLine("State2 variable: " + state2.Variables["result"].Value);
}

public static async void ScriptWithNamespace()
{
    var scriptOptions = ScriptOptions.Default.AddNamespaces("System.IO");
    var script = CSharpScript.Create(@"var result = Path.Combine(""folder"", ""file"");", scriptOptions);
    var state = await script.RunAsync();
    Console.WriteLine("State variable: " + state.Variables["result"].Value);
}

public static async void ScriptAsDelegate()
{
    var param = new SumParams { a = 2, b = 22 };
    var script = CSharpScript.Create<int>(@"int sum(int a, int b){return a + b;}", ScriptOptions.Default, typeof(SumParams))
        .ContinueWith("sum(a, b)");
    var function = script.CreateDelegate();
    var result = await function(param);
    Console.WriteLine("Sum function: " + result);
}

Comments


Comments are closed