-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSharp.Scripting.csx
50 lines (44 loc) · 1.97 KB
/
CSharp.Scripting.csx
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
49
50
#r "nuget: CScripting, *"
#r "nuget: Microsoft.CodeAnalysis.CSharp.Scripting, *"
using static CScripting;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Scripting;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.IO;
var so = ScriptOptions.Default.WithImports("System.IO");
so.WithImports("System");
Console.WriteLine(@"测试输入输出函数:Directory.GetCurrentDirectory()");
var res = await CSharpScript.EvaluateAsync(@"
using System;
Console.WriteLine(10086);
Directory.GetCurrentDirectory()", so);// *if end with ; no return value
Console.WriteLine(res);
var codeText = File.ReadAllText(@"./CSharp.Scripting.codetext.csx");
codeText = codeText.Insert(0, "using static CScripting;\r\n");
codeText = codeText.Insert(0, "using static CScripting;\r\n");
var scriptOptions = ScriptOptions.Default
.AddReferences(typeof(CScripting).Assembly);
scriptOptions.AddReferences(typeof(System.Math).Assembly);
SyntaxTree tree = CSharpSyntaxTree.ParseText(codeText);
var script = CSharpScript.Create(codeText, scriptOptions);
/*
public ScriptRunner<T> CreateDelegate(CancellationToken cancellationToken = default);
public Task<ScriptState<T>> RunAsync(object globals, CancellationToken cancellationToken);
public Task<ScriptState<T>> RunAsync(object globals = null, Func<Exception, bool> catchException = null, CancellationToken cancellationToken = default);
public Task<ScriptState<T>> RunFromAsync(ScriptState previousState, CancellationToken cancellationToken);
public Task<ScriptState<T>> RunFromAsync(ScriptState previousState, Func<Exception, bool> catchException = null, CancellationToken cancellationToken = default);
public Script<T> WithOptions(ScriptOptions options);
*/
try
{
script.Compile();
res = await script.RunAsync();
print(res);
}
catch (System.Exception codeError)
{
print(codeError.Message);
// print(codeError.StackTrace);
}