forked from dotnet/roslyn
-
Notifications
You must be signed in to change notification settings - Fork 7
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.
- Evaluate a C# expression
- Evaluate a C# expression (strongly-typed)
- Evaluated a C# expression with error handling
- Add references
- Add imports
- Parameterize a script
- Create & build a C# script and execute it multiple times
- Create a delegate to a script
- Run a C# snippet and inspect defined script variables
- Chain code snippets to form a script
- Continue script execution from a previous state
- Create and analyze a C# script
- Customize assembly loading
object result = await CSharpScript.EvaluateAsync("1 + 2");
int result = await CSharpScript.EvaluateAsync<int>("1 + 2");
try
{
Console.WriteLine(await CSharpScript.EvaluateAsync(Console.ReadLine()));
}
catch (CompilationErrorException e)
{
Console.WriteLine(string.Join(Environment.NewLine, e.Diagnostics));
}
var result = await CSharpScript.EvaluateAsync("MyLib.DoStuff()",
ScriptOptions.Default.WithReferences(typeof(Lib).Assembly));
var result = await CSharpScript.EvaluateAsync("Sqrt(2)",
ScriptOptions.Default.WithImports("System.Math"));
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));
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 }));
}
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 });
}
var state = await CSharpScript.RunAsync<int>(Console.ReadLine());
foreach (var variable in state.Variables)
Console.WriteLine($"{variable.Name} = {variable.Value} of type {variable.Type}");
var script = CSharpScript.
Create<int>("int x = 1;").
ContinueWith("int y = 2;").
ContinueWith("x + y");
Console.WriteLine(await script.EvaluateAsync());
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);
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.
using Microsoft.CodeAnalysis.Scripting.Hosting;
using (var loader = new InteractiveAssemblyLoader())
{
var script = CSharpScript.Create<int>("1", assemblyLoader: loader);
//do stuff
}
#Contributors Building, Testing, and Debugging
#Tool Authors Getting Started
#Status Language feature status
Edit-and-Continue Supported Edits
#Interactive Docs Interactive Window Documentation