Repository navigation
Expand file tree
/
Copy pathCSharpScript.cs
More file actions
102 lines (94 loc) · 5.97 KB
/
Copy pathCSharpScript.cs
File metadata and controls
102 lines (94 loc) · 5.97 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Scripting;
using Microsoft.CodeAnalysis.Scripting.Hosting;
namespace Microsoft.CodeAnalysis.CSharp.Scripting
{
/// <summary>
/// A factory for creating and running C# scripts.
/// </summary>
public static class CSharpScript
{
/// <summary>
/// Create a new C# script.
/// </summary>
/// <param name="code">The source code of the script.</param>
/// <param name="options">The script options.</param>
/// <param name="globalsType">Type of global object.</param>
/// <param name="assemblyLoader">Custom assembly loader.</param>
/// <typeparam name="T">The return type of the script</typeparam>
public static Script<T> Create<T>(string code, ScriptOptions options = null, Type globalsType = null, InteractiveAssemblyLoader assemblyLoader = null)
{
return Script.CreateInitialScript<T>(CSharpScriptCompiler.Instance, code, options, globalsType, assemblyLoader);
}
/// <summary>
/// Create a new C# script.
/// </summary>
/// <param name="code">The source code of the script.</param>
/// <param name="options">The script options.</param>
/// <param name="globalsType">Type of global object.</param>
/// <param name="assemblyLoader">Custom assembly loader.</param>
public static Script<object> Create(string code, ScriptOptions options = null, Type globalsType = null, InteractiveAssemblyLoader assemblyLoader = null)
{
return Create<object>(code, options, globalsType, assemblyLoader);
}
/// <summary>
/// Run a C# script.
/// </summary>
/// <param name="code">The source code of the script.</param>
/// <param name="options">The script options.</param>
/// <param name="globals">An object instance whose members can be accessed by the script as global variables.</param>
/// <param name="globalsType">Type of global object, <paramref name="globals"/>.GetType() is used if not specified.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <typeparam name="T">The return type of the submission</typeparam>
/// <exception cref="CompilationErrorException">Specified code has errors.</exception>
public static Task<ScriptState<T>> RunAsync<T>(string code, ScriptOptions options = null, object globals = null, Type globalsType = null, CancellationToken cancellationToken = default(CancellationToken))
{
return Create<T>(code, options, globalsType ?? globals?.GetType()).RunAsync(globals, cancellationToken);
}
/// <summary>
/// Run a C# script.
/// </summary>
/// <param name="code">The source code of the script.</param>
/// <param name="options">The script options.</param>
/// <param name="globals">An object instance whose members can be accessed by the script as global variables.</param>
/// <param name="globalsType">Type of global object, <paramref name="globals"/>.GetType() is used if not specified.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <exception cref="CompilationErrorException">Specified code has errors.</exception>
public static Task<ScriptState<object>> RunAsync(string code, ScriptOptions options = null, object globals = null, Type globalsType = null, CancellationToken cancellationToken = default(CancellationToken))
{
return RunAsync<object>(code, options, globals, globalsType, cancellationToken);
}
/// <summary>
/// Run a C# script and return its resulting value.
/// </summary>
/// <param name="code">The source code of the script.</param>
/// <param name="options">The script options.</param>
/// <param name="globals">An object instance whose members can be accessed by the script as global variables.</param>
/// <param name="globalsType">Type of global object, <paramref name="globals"/>.GetType() is used if not specified.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <typeparam name="T">The return type of the submission</typeparam>
/// <return>Returns the value returned by running the script.</return>
/// <exception cref="CompilationErrorException">Specified code has errors.</exception>
public static Task<T> EvaluateAsync<T>(string code, ScriptOptions options = null, object globals = null, Type globalsType = null, CancellationToken cancellationToken = default(CancellationToken))
{
return RunAsync<T>(code, options, globals, globalsType, cancellationToken).GetEvaluationResultAsync();
}
/// <summary>
/// Run a C# script and return its resulting value.
/// </summary>
/// <param name="code">The source code of the script.</param>
/// <param name="options">The script options.</param>
/// <param name="globals">An object instance whose members can be accessed by the script as global variables.</param>
/// <param name="globalsType">Type of global object, <paramref name="globals"/>.GetType() is used if not specified.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <return>Returns the value returned by running the script.</return>
/// <exception cref="CompilationErrorException">Specified code has errors.</exception>
public static Task<object> EvaluateAsync(string code, ScriptOptions options = null, object globals = null, Type globalsType = null, CancellationToken cancellationToken = default(CancellationToken))
{
return EvaluateAsync<object>(code, options, globals, globalsType, cancellationToken);
}
}
}