RuntimeCompiler 1.0.0
See the version list below for details.
dotnet add package RuntimeCompiler --version 1.0.0
NuGet\Install-Package RuntimeCompiler -Version 1.0.0
<PackageReference Include="RuntimeCompiler" Version="1.0.0" />
paket add RuntimeCompiler --version 1.0.0
#r "nuget: RuntimeCompiler, 1.0.0"
// Install RuntimeCompiler as a Cake Addin #addin nuget:?package=RuntimeCompiler&version=1.0.0 // Install RuntimeCompiler as a Cake Tool #tool nuget:?package=RuntimeCompiler&version=1.0.0
RuntimeCompiler
RuntimeCompiler
affers easy to use Methods which will compile C# code to an Action
or Func
at runtime.
It uses the Roslyn Compiler platform to acchieve the outcome, so the result is just the same as if you had written code and compiled it down to a normal Assembly
.
Usage
Basic
var action = RuntimeCompiler.CompileAction("Console.WriteLine(\"Hello from my dynamic action!\")");
action(); // Output: Hello from my dynamic action!
var func = RuntimeCompiler.CompileFunction<int>("1");
var result = func(1); // result is 1
var greeterFunc = Runtimpiler.CompileFunction<string, string>("\"Hello \" + it");
var greeting = func("World") // greeting is "Hello World"
var sumFunc = RuntimeCompiler.CompileFunction<int, int, int>("x + y", new { "x", "y"});
var sum = func(1, 2); // sum is 3
With method body
var methodBody = @"
for (int i = 0; i < times; i++)
{
Console.WriteLine(""Hello "" + name);
}
";
var multiGreeter = RuntimeCompiler.CompileAction<string, int>(methodBody, new [] { "name", "times" });
multiGreeter("World", 10); // outputs "Hello World" ten times
Custom types
The System
namespace and all namespaces from parameter or return types are automatically added as using statements and the appropriate Assemblies are automatically imported too.
If you need additional types (and usings), you can supply these as parameters.
namespace MyNamespace
{
public class MyClass
{
public int Foo { get; set; }
}
}
// ...
var methodBody = @"
var x = new MyClass();
x.Foo = 3;
Console.WriteLine(x.Foo);
";
var action = Compiler.CompileAction(methodBody, "using MyNamespace;");
action();
Going deeper
If you ned even more control, you can use Compiler.CompileMethod(...)
or Compiler.CompileAssembly(...)
.
Warning
With great power comes great responsibility! This will inject code into your running application so it opens up a very high risk for attackers to compromise your systems. Use it only in environments where you have full control over the inputs given. You must never allow any unverified user input to be passed in!
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net5.0
- Microsoft.CodeAnalysis.CSharp (>= 3.9.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
5.0.0 | 90 | 11/19/2024 |
4.3.0 | 402 | 7/10/2024 |
4.2.0 | 110 | 5/24/2024 |
4.1.0 | 505 | 1/9/2024 |
4.0.0 | 233 | 11/14/2023 |
3.3.0 | 195 | 9/5/2023 |
3.2.0 | 328 | 6/14/2023 |
3.1.0 | 283 | 4/18/2023 |
3.0.1 | 616 | 11/9/2022 |
3.0.0 | 314 | 11/9/2022 |
2.0.2 | 444 | 10/10/2022 |
2.0.1 | 424 | 8/16/2022 |
2.0.0 | 370 | 8/16/2022 |
1.0.1 | 380 | 8/16/2022 |
1.0.0 | 358 | 5/14/2021 |