You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+103-9Lines changed: 103 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,17 +1,37 @@
1
1
# Westwind CSharp Scripting
2
2
### Dynamically compile and execute CSharp code at runtime
3
3
4
-
This small `CSharpScripting` class provides an easy way to compile and execute C# on the fly at runtime using the .NET compiler services. This is ideal to provide support for addin's and application automation tasks that are user configurable.
4
+
<small>for .NET 4.52 and later</small>
5
+
6
+
The small `CSharpScripting` class provides an easy way to compile and execute C# on the fly from source code at runtime using the .NET compiler services on full Framework .NET. You can use Roslyn compilation for the latest C# features, or classic C# 5 features.
7
+
8
+
This class makes is very easy to integrate simple scripting or text merging features into applications with minimal effort.
9
+
10
+
This library provides:
11
+
12
+
**Execution Features**
13
+
14
+
*`ExecuteCode()` - Execute an arbitrary block of code
15
+
*`Evaluate()` - Evaluate an expression from a code string
16
+
*`ExecuteMethod()` - Execute one or more methods from source
17
+
*`CompileClass()` - Generate a class instance from C# code
18
+
19
+
**Support features**
20
+
21
+
* Assembly Caching so not every execution generates a new assembly
22
+
* Ability to compile entire classes and execute them
23
+
* Automatic Assembly Cleanup at shutdown
24
+
* Use Roslyn or Classic C# compiler interchangably
25
+
* Display errors and source and line numbers
5
26
6
-
The library supports both the latest Roslyn compiler and classic CSharp compilation.
7
27
8
28
> #### Requires Roslyn Code Providers for your Project
9
-
> If you want to use Roslyn compilation you have to make sure you add the `Microsoft.CodeDom.CompilerServices` NuGet Package to your project to provide the required compiler binaries for your application. This should be added to the application's start project.
29
+
> If you want to use Roslyn compilation for the latest C# features you have to make sure you add the `Microsoft.CodeDom.CompilerServices` NuGet Package to your application's root project to provide the required compiler binaries for your application.
10
30
>
11
-
> Note that this adds a sizable chunk of files to your application's output folder in the `roslyn` folder. If you don't want this you can use the classic compiler, at the cost of not having access to C# 6+ features.
31
+
> Note that this adds a sizable chunk of files to your application's output folder in the `\roslyn` folder. If you don't want this you can use the classic compiler, at the cost of not having access to C# 6+ features.
12
32
13
33
## Usage
14
-
Using the `CSharpScriptExecution` class is very easy. It works by letting you provide either a simple code snippet that can optionally `return` a value, or a complete method signature with a method header and return statement. You can also provide multiple method that can be called explicitly using the `InvokeMethod()` operation.
34
+
Using the `CSharpScriptExecution` class is very easy. It works with code passed as strings for either a Code block, expression, one or more methods or even as a full C# class that can be turned into an instance.
15
35
16
36
You can add **Assembly References** and **Namespaces** via the `AddReferece()` and `AddNamespace()` methods.
Note that the `return` in your code snippet is optional - you can just run code without a result value. Any parameters you pass in can be accessed either via `parameters[0]`, `parameters[1]` etc. or using a simpler string representation of `@0`, `@1`.
82
+
Note that the `return` in your code snippet is optional - you can just run code without a result value.
83
+
84
+
> Any parameters you pass in can be accessed either via `parameters[0]`, `parameters[1]` etc. or using a simpler string representation of `@0`, `@1`.
63
85
64
86
### Evaluating an expression
65
87
If you want to evaluate a single expression, there's a shortcut `Evalute()` method that works pretty much the same:
This method is a shortcut wrapper and simply wraps your code into a single line `return {exp};` statement.
111
+
This method is a shortcut wrapper and simply wraps your code into a single line `return {exp};` statement.
90
112
91
113
### Executing a Method
92
-
Another way to execute code is to provide a full method body which is a little more explicit and makes it easier to reference parameters passed in.
114
+
`ExecuteCode()` and `Evaluate()` are shortcuts for the slightly lower level and more flexible `ExecuteMethod()` method which as the name implies allows you to specify a single or multiple methods. In fact you can provide an **entire class body** including properties, events and nested class definitions in the code passed in. This gives a lot of flexibility as you can properly type parameters and return types:
93
115
94
116
```csharp
95
117
varscript=newCSharpScriptExecution()
@@ -165,4 +187,76 @@ result = instance.GoodbyeWorld();
165
187
166
188
Console.WriteLine($"Result: {result}");
167
189
Assert.IsTrue(result.Contains("Goodbye Markus"));
168
-
```
190
+
```
191
+
192
+
### Compiling and Executing a Class
193
+
You can also compile an **entire class** and then get passed back a `dynamic` reference to that class so that you can explicitly use that object:
194
+
195
+
196
+
```cs
197
+
varscript=newCSharpScriptExecution()
198
+
{
199
+
SaveGeneratedCode=true,
200
+
CompilerMode=ScriptCompilerModes.Roslyn
201
+
};
202
+
script.AddDefaultReferencesAndNamespaces();
203
+
204
+
varcode=$@"
205
+
using System;
206
+
207
+
namespace MyApp
208
+
{{
209
+
public class Math
210
+
{{
211
+
public string Add(int num1, int num2)
212
+
{{
213
+
// string templates
214
+
var result = num1 + "" + "" + num2 + "" = "" + (num1 + num2);
215
+
Console.WriteLine(result);
216
+
217
+
return result;
218
+
}}
219
+
220
+
public string Multiply(int num1, int num2)
221
+
{{
222
+
// string templates
223
+
var result = $""{{num1}} * {{num2}} = {{(num1 * num2)}}"";
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
259
+
260
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
261
+
262
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0 commit comments