-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathProgram.cs
More file actions
126 lines (106 loc) · 5.66 KB
/
Program.cs
File metadata and controls
126 lines (106 loc) · 5.66 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using static Microsoft.CodeAnalysis.CSharp.SyntaxTokenParser;
using CSScripting;
using CSScriptLib;
namespace Client.NET472
{
class Program
{
static void Main(string[] args)
{
PrepareCodeDomCompilers();
Console.WriteLine($"Hosting runtime: .NET {(Runtime.IsCore ? "Core" : "Framework")}");
Test_CodeDom();
Test_CodeDom_GAC();
Test_CodeDom_CSharp7();
Test_Roslyn_Eval();
Test_Roslyn();
}
static void Test_CodeDom()
{
dynamic script = CSScript.CodeDomEvaluator
.LoadMethod(@"public object func()
{
return new[]{0,5}; // C# 5 syntax
}");
var result = script.func();
Console.WriteLine($"CodeDom: {result}");
}
static void Test_CodeDom_GAC()
{
// System.Net.Http.dll needs t be referenced from GAC so we need to add its location to the probing dir
dynamic script = CSScript.CodeDomEvaluator
.LoadCode(@"//css_dir C:\Windows\Microsoft.NET\assembly\GAC_MSIL\**
//css_ref System.Net.Http.dll
using System;
using System.Net.Http;
public class Test_CodeDom
{
public void Foo()
{
using (var client = new HttpClient())
{
Console.WriteLine(""CodeDom + GAC: Test_CodeDom.Foo()"");
}
}
}");
script.Foo();
}
static void Test_CodeDom_CSharp7()
{
dynamic script = CSScript.CodeDomEvaluator
.LoadMethod(@"public object func()
{
return (0,5); // C# 7.3 syntax
}");
var result = script.func();
Console.WriteLine($"CodeDom + C# 7.3: {result}");
}
static void Test_Roslyn_Eval()
{
// This approach is rather simplistic. Use it only if you have to.
int sum = CSScript.RoslynEvaluator.Eval("6 + 3");
Console.WriteLine($"Roslyn + Eval: {sum}");
}
static void Test_Roslyn()
{
dynamic script = CSScript.RoslynEvaluator
.LoadMethod(@"public (int, int) func()
{
return (0,5);
}");
(int, int) result = script.func();
Console.WriteLine($"Roslyn: {result}");
}
static void PrepareCodeDomCompilers()
{
// The default C# compiler that comes with Windows supports only C# 5 syntax.
// For compiling C# 7.3 syntax you will need to install a newer one.
// You can install the newer compiler by either installing .NET SDK or downloading SDK tools NuGet package.
// If you want to download the compiler from nuget.org either manually from
// https://api.nuget.org/v3-flatcontainer/microsoft.net.compilers.toolset/5.0.0/microsoft.net.compilers.toolset.5.0.0.nupkg
// or by using CS-Script's own downloader: Uncomment next two lines:
//
// NugetPackageDownloader.OnProgressOutput = Console.WriteLine;
// NugetPackageDownloader.DownloadLatestFrameworkCompiler(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 initialized internally the same way. Providing it here for demo purposes only.
Globals.csc =
Globals.FindFrameworkToolsetPackageCompiler() ?? // lookup the installed nuget package with the compiler
Globals.FindDefaultFrameworkCompiler(); // the default compiler, which is a part of Windows OS
// ========================================================
// RUNTIME: Note, when running the first time the loading overhead of csc.exe is noticeable but on the subsequent runs it becomes up to 10
// times faster as csc.exe is already loaded in memory. It stays loaded even after the host application is restarted.
// It is .NET own caching mechanism that keeps the compiler loaded in memory and it is not related to CS-Script.
//
// DEPLOYMENT: If you need to deploy your application to an environment where Microsoft.Net.Compilers.Toolset package is not available you can
// copy csc.exe and its dependencies to the same folder as your application and set the path to csc.exe in Globals.csc.
}
}
}