-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathProgram.cs
More file actions
179 lines (147 loc) · 7.32 KB
/
Program.cs
File metadata and controls
179 lines (147 loc) · 7.32 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using CSScripting;
using CSScriptLib;
namespace Client.NET
{
public class Program
{
static void Main(string[] args)
{
PrepareCodeDomCompilers();
Console.WriteLine("================\n");
Console.WriteLine($"Loading and unloading script 20 times");
Test_Unloading();
Console.WriteLine("================\n");
CSScript.StopBuildServer();
CSScript.EvaluatorConfig.DebugBuild = true;
var sw = Stopwatch.StartNew();
Console.WriteLine($"Hosting runtime: .NET {(Runtime.IsCore ? "Core" : "Framework")}");
Console.WriteLine("================\n");
Console.WriteLine("CodeDOM");
Test_CodeDom();
Console.WriteLine(" first run: " + sw.ElapsedMilliseconds);
sw.Restart();
Test_CodeDom();
Console.WriteLine(" next run: " + sw.ElapsedMilliseconds);
Console.WriteLine("\nRoslyn");
sw.Restart();
Test_Roslyn();
Console.WriteLine(" first run: " + sw.ElapsedMilliseconds);
sw.Restart();
Test_Roslyn();
Console.WriteLine(" next run: " + sw.ElapsedMilliseconds);
}
static void Test_CodeDom()
{
dynamic script = CSScript.CodeDomEvaluator
.LoadMethod(@"public (int, int) func()
{
return (0,5);
}");
(int, int) result = script.func();
}
static void Test_Roslyn()
{
dynamic script = CSScript.RoslynEvaluator
.LoadMethod(@"public (int, int) func()
{
return (0,5);
}");
(int, int) result = script.func();
}
static void call_UnloadAssembly()
{
CSScript.EvaluatorConfig.PdbFormat = Microsoft.CodeAnalysis.Emit.DebugInformationFormat.Embedded;
CSScript.EvaluatorConfig.DebugBuild = true;
var script = CSScript.Evaluator
.With(eval => eval.IsAssemblyUnloadingEnabled = true)
.LoadMethod<ICalc>(@"public int Sum(int a, int b)
{ return a+b; }");
script.Sum(1, 2);
script.GetType().Assembly.Unload();
}
static Assembly printer_asm;
static void call_UnloadAssemblyWithDependency()
{
var info = new CompileInfo { RootClass = "Printing", AssemblyFile = "Printer.dll", AssemblyName = "PrintAsm" };
if (printer_asm == null)
printer_asm = CSScript.Evaluator
.CompileCode(@"using System;
public class Printer
{
public static void Print() =>
Console.WriteLine(""Printing..."");
}", info);
var script = CSScript.Evaluator
.With(eval => eval.IsAssemblyUnloadingEnabled = true)
.ReferenceAssembly(printer_asm)
.LoadMethod<ICalc>(@"public int Sum(int a, int b)
{
Printing.Printer.Print();
return a+b;
}");
script.Sum(1, 2);
script.GetType().Assembly.Unload();
}
static void call_UnloadAssembly_Failing()
{
// using 'dynamic` completely breaks CLR unloading mechanism. Most likely it triggers an
// accidental referencing of the assembly or System.Runtime.Loader.AssemblyLoadContext.
dynamic script = CSScript.Evaluator
.With(eval => eval.IsAssemblyUnloadingEnabled = true)
.LoadMethod(@"public int Sum(int a, int b)
{ return a+b; }");
script.Sum(1, 2);
(script as object).GetType().Assembly.Unload();
}
static void call_UnloadAssembly_Crashing_CLR()
{
CSScript.EvaluatorConfig.PdbFormat = Microsoft.CodeAnalysis.Emit.DebugInformationFormat.Embedded;
CSScript.EvaluatorConfig.DebugBuild = true;
var script = CSScript.Evaluator
.With(eval => eval.IsAssemblyUnloadingEnabled = true)
.LoadMethod<ICalc>(@"public int Sum(int a, int b)
{ return a+b; }");
script.Sum(1, 2);
GC.Collect(); // see https://github.com/oleg-shilo/cs-script/issues/301 for details
script.GetType().Assembly.Unload();
}
static void Test_Unloading()
{
for (int i = 0; i < 20; i++)
{
Console.WriteLine("Loaded assemblies count: " + AppDomain.CurrentDomain.GetAssemblies().Count());
call_UnloadAssembly();
//call_UnloadAssemblyWithDependency(); // also works OK; provided just for demo
// call_UnloadAssembly_Failing();
// call_UnloadAssembly_Crashing_CLR();
GC.Collect();
}
}
static void PrepareCodeDomCompilers()
{
// If you are using CodeDom evaluator and your hosting environment does not have .NET SDK installed
// you will need to install the compiler by downloading SDK tools NuGet package.
// Either manually from
// https://api.nuget.org/v3-flatcontainer/microsoft.net.sdk.compilers.toolset/10.0.103/microsoft.net.sdk.compilers.toolset.10.0.103.nupkg
// Or by executing `css -deploy-csc` command in the terminal, which will download and extract the package to the default location.
// Or by using CS-Script's own downloader. Uncomment next two lines:
//
// NugetPackageDownloader.OnProgressOutput = Console.WriteLine;
// NugetPackageDownloader.DownloadLatestSdkCompiler(includePrereleases: false);
// This sample works even if you do not uncomment the code above because the compiling tools package is added to this
// project.
// Globals.csc is internally initialized the same way. Providing it here for demo purposes only.
Globals.csc =
Globals.FindSdKCompiler() ?? // or from .NET SDK installed on OS
Globals.FindSdkToolsetPackageCompiler(includePrereleases: false); // from the installed Microsoft.Net.Sdk.Compilers.Toolset package
}
}
public interface ICalc
{
int Sum(int a, int b);
}
}