Skip to content

Scripting API Samples

Kasey Uhlenhuth edited this page Oct 19, 2015 · 26 revisions

The scripting APIs enable .NET applications to instatiate a C# engine and execute code snippets against host-supplied objects. Below are examples of how to get started with the scripting APIs and some common samples.

Note: most samples require the following usings:
using Microsoft.CodeAnalysis.CSharp.Scripting;

You can also view the source code. There may be some changes to the API after it was finalized early October 2015.

Scenarios


Evaluate a C# expression

object result = await CSharpScript.EvaluateAsync("1 + 2");

Evaluate a C# expression (strongly-typed)

int result = await CSharpScript.EvaluateAsync<int>("1 + 2");

Evaluate a C# expression with error handling

try
{
    Console.WriteLine(await CSharpScript.EvaluateAsync(Console.ReadLine()));
}
catch (CompilationErrorException e)
{
    Console.WriteLine(string.Join(Environment.NewLine, e.Diagnostics));
}

Add references

var result = await CSharpScript.EvaluateAsync("MyLib.DoStuff()", 
                         ScriptOptions.Default.WithReferences(typeof(Lib).Assembly));

Add imports

var result = await CSharpScript.EvaluateAsync("Sqrt(2)", 
                ScriptOptions.Default.WithImports("System.Math"));

Parameterize a script

public class Globals
{
    public int X;
    public int Y;
}
var globals = new Globals { X = 1, Y = 2 };
Console.WriteLine(await CSharpScript.EvaluateAsync<int>("X+Y", globals: globals));

Create & build a C# script and execute it multiple times

var script = CSharpScript.Create<int>("X*Y", globalsType: typeof(Globals));
script.Build();
for (int i = 0; i < 10; i++)
{
    Console.WriteLine(await script.EvaluateAsync(new Globals { X = i, Y = i }));
}

Create a delegate to a script

The delegate doesn’t hold compilation resources (syntax trees, etc.) alive.

var script = CSharpScript.Create<int>("X*Y", globalsType: typeof(Globals));
ScriptRunner<int> runner = script.CreateDelegate();
for (int i = 0; i < 10; i++)
{
    await runner(new Globals { X = i, Y = i });
}

Run a C# snippet and inspect defined script variables

var state = await CSharpScript.RunAsync<int>(Console.ReadLine());
foreach (var variable in state.Variables)
     Console.WriteLine($"{variable.Name} = {variable.Value} of type {variable.Type}");

Chain code snippets to form a script

var script = CSharpScript.
    Create<int>("int x = 1;").
    ContinueWith("int y = 2;").
    ContinueWith("x + y");

Console.WriteLine(await script.EvaluateAsync());

Continue script execution from a previous state

var state = await CSharpScript.RunAsync("int x = 1;");
state = await state.ContinueWithAsync("int y = 2;");
state = await state.ContinueWithAsync("x+y");
Console.WriteLine(state.ReturnValue);

Create and analyze a C# script

using Microsoft.CodeAnalysis;
var script = CSharpScript.Create<int>(Console.ReadLine());
Compilation compilation = script.GetCompilation();
//do stuff

Compilation gives access to the full set of Roslyn APIs.

Customize assembly loading

using Microsoft.CodeAnalysis.Scripting.Hosting;
using (var loader = new InteractiveAssemblyLoader())
{
    var script = CSharpScript.Create<int>("1", assemblyLoader: loader);
    //do stuff 
}
Clone this wiki locally