-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
48 lines (39 loc) · 1.36 KB
/
Program.cs
File metadata and controls
48 lines (39 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using Microsoft.CodeAnalysis.Scripting;
using NET_SandboxExperiments;
using NET_SandboxExperiments.Builtins;
using System.Reflection;
// Define globals for the script
var globals = new DslGlobals { str = "hello" };
// Allow only the functions in IDslBuiltins
// Real functions are implemented in DslBuiltins
var allowedFunctions = new List<Type> { typeof(IDslBuiltins) };
var allowedFunctionsImpl = new List<Type> { typeof(DslBuiltins) };
// Get the assemblies from the builtins class
var allowedAssemblies = allowedFunctions.Select(x => x.Assembly).ToList();
// TODO: There's probably a better way to do this, but i'm not sure
var withImports = allowedFunctionsImpl.Select(x => $"{x.Namespace}.{x.Name}" ).ToList();
ScriptRunner<bool>? script = null;
// Create the C# script with our allowed functions
// This will throw an ArgumentException if it fails to validate.
try
{
script = DSL.VerifyAndCreateCSharpScript("len(str) == 5", typeof(DslGlobals), allowedFunctions, allowedAssemblies, withImports);
}
catch (ArgumentException ex)
{
Console.WriteLine($"Failed to verify C# Script: {ex}");
return;
}
catch (Exception ex)
{
Console.WriteLine($"Failed to create C# Script: {ex}");
return;
}
// Call the created script
bool scriptResult = await script(globals);
Console.WriteLine(scriptResult);
// Globals we use in the script
public class DslGlobals
{
public string str;
}