Ecng.Compilation 1.0.287

dotnet add package Ecng.Compilation --version 1.0.287
                    
NuGet\Install-Package Ecng.Compilation -Version 1.0.287
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Ecng.Compilation" Version="1.0.287" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Ecng.Compilation" Version="1.0.287" />
                    
Directory.Packages.props
<PackageReference Include="Ecng.Compilation" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Ecng.Compilation --version 1.0.287
                    
#r "nuget: Ecng.Compilation, 1.0.287"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Ecng.Compilation@1.0.287
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Ecng.Compilation&version=1.0.287
                    
Install as a Cake Addin
#tool nuget:?package=Ecng.Compilation&version=1.0.287
                    
Install as a Cake Tool

Ecng.Compilation

Dynamic code compilation infrastructure for C#, F#, and Python. Compile code at runtime, manage references, and cache assemblies.

Overview

This library provides a unified interface for runtime code compilation supporting multiple languages through specialized providers.

Core Types

ICompiler Interface

The main abstraction for compilers.

public interface ICompiler
{
    string Extension { get; }           // e.g., ".cs", ".fs", ".py"
    bool IsAssemblyPersistable { get; } // Can save compiled assembly
    bool IsTabsSupported { get; }       // Language supports tabs
    bool IsCaseSensitive { get; }       // Language is case-sensitive
    bool IsReferencesSupported { get; } // Supports external references

    ICompilerContext CreateContext();

    Task<CompilationResult> Compile(
        string name,
        IEnumerable<string> sources,
        IEnumerable<(string name, byte[] body)> refs,
        CancellationToken ct = default);

    Task<CompilationError[]> Analyse(
        object analyzer,
        IEnumerable<object> analyzerSettings,
        string name,
        IEnumerable<string> sources,
        IEnumerable<(string name, byte[] body)> refs,
        CancellationToken ct = default);
}

CompilationResult

Result of a compilation operation.

var result = await compiler.Compile("MyCode", sources, references);

if (result.HasErrors)
{
    foreach (var error in result.Errors)
    {
        Console.WriteLine($"{error.Type}: {error.Message}");
        Console.WriteLine($"  Line {error.Line}, Column {error.Column}");
    }
}
else
{
    // Get compiled assembly
    Assembly assembly = result.Assembly;

    // Or get as bytes for storage
    byte[] assemblyBytes = result.AssemblyBytes;
}

CompilationError

Represents a compilation error or warning.

public class CompilationError
{
    public CompilationErrorTypes Type { get; }  // Error, Warning, Info
    public string Id { get; }                    // e.g., "CS0001"
    public string Message { get; }
    public int Line { get; }
    public int Column { get; }
}

Reference Types

AssemblyReference

Reference to a .NET assembly.

// From file path
var fileRef = new AssemblyReference("path/to/assembly.dll");

// From loaded assembly
var loadedRef = new AssemblyReference(typeof(SomeClass).Assembly);

NuGetReference

Reference to a NuGet package.

var nugetRef = new NuGetReference
{
    PackageId = "Newtonsoft.Json",
    Version = "13.0.1"
};

Compiler Provider

Manages available compilers and provides access to them.

// Get registered compilers
IEnumerable<ICompiler> compilers = CompilerProvider.Compilers;

// Get compiler by extension
ICompiler csCompiler = CompilerProvider.GetCompiler(".cs");
ICompiler fsCompiler = CompilerProvider.GetCompiler(".fs");
ICompiler pyCompiler = CompilerProvider.GetCompiler(".py");

Compiler Implementations

Package Language Extension
Ecng.Compilation.Roslyn C# .cs
Ecng.Compilation.FSharp F# .fs
Ecng.Compilation.Python Python .py

Expression Formulas

Parse and evaluate mathematical expressions at runtime.

using Ecng.Compilation.Expressions;

// Parse expression
var formula = ExpressionHelper.Parse("(a + b) * 2");

// Get variables used
IEnumerable<string> vars = formula.Variables; // ["a", "b"]

// Evaluate with values
var values = new Dictionary<string, decimal>
{
    ["a"] = 10,
    ["b"] = 5
};
decimal result = formula.Calculate(values); // 30

Supported Operations

  • Arithmetic: +, -, *, /, %
  • Comparison: <, >, <=, >=, ==, !=
  • Logical: &&, ||, !
  • Functions: abs, sqrt, min, max, pow, etc.

Assembly Load Context

Manage assembly loading for isolation.

using Ecng.Compilation;

// Track loaded assemblies
var tracker = new AssemblyLoadContextTracker();

// Load assembly in isolated context
var context = tracker.CreateContext("MyPlugin");
Assembly asm = context.LoadFromAssemblyPath("plugin.dll");

// Unload when done
tracker.Unload("MyPlugin");

Compiler Cache

Cache compiled assemblies for reuse.

public interface ICompilerCache
{
    bool TryGet(string key, out byte[] assembly);
    void Set(string key, byte[] assembly);
    void Remove(string key);
    void Clear();
}

Usage Examples

Compile C# Code

var compiler = CompilerProvider.GetCompiler(".cs");

string code = @"
public class Calculator
{
    public int Add(int a, int b) => a + b;
}";

var result = await compiler.Compile(
    name: "Calculator",
    sources: new[] { code },
    refs: Array.Empty<(string, byte[])>());

if (!result.HasErrors)
{
    var type = result.Assembly.GetType("Calculator");
    var instance = Activator.CreateInstance(type);
    var method = type.GetMethod("Add");
    int sum = (int)method.Invoke(instance, new object[] { 2, 3 });
    Console.WriteLine(sum); // 5
}

Compile with References

var refs = new List<(string, byte[])>();

// Add reference
var asmBytes = File.ReadAllBytes("MyLibrary.dll");
refs.Add(("MyLibrary.dll", asmBytes));

var result = await compiler.Compile("MyCode", sources, refs);

NuGet

Install-Package Ecng.Compilation
Install-Package Ecng.Compilation.Roslyn   # For C# support
Install-Package Ecng.Compilation.FSharp   # For F# support
Install-Package Ecng.Compilation.Python   # For Python support
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (7)

Showing the top 5 NuGet packages that depend on Ecng.Compilation:

Package Downloads
StockSharp.Algo

Trading algorithms. More info on web site https://stocksharp.com/store/

Ecng.Compilation.Roslyn

Ecng system framework

Ecng.Roslyn

Ecng system framework

Ecng.Compilation.Python

Ecng system framework

Ecng.Compilation.FSharp

Ecng system framework

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Ecng.Compilation:

Repository Stars
StockSharp/StockSharp
Algorithmic trading and quantitative trading open source platform to develop trading robots (stock markets, forex, crypto, bitcoins, and options).
Version Downloads Last Updated
1.0.287 161 1/26/2026
1.0.286 144 1/26/2026
1.0.285 163 1/22/2026
1.0.284 153 1/22/2026
1.0.283 205 1/21/2026
1.0.282 252 1/19/2026
1.0.281 190 1/19/2026
1.0.280 182 1/18/2026
1.0.279 177 1/18/2026
1.0.278 198 1/16/2026
1.0.277 259 1/14/2026
1.0.276 192 1/13/2026
1.0.275 195 1/13/2026
1.0.274 199 1/12/2026
1.0.273 288 1/9/2026
1.0.272 235 1/9/2026
1.0.271 189 1/8/2026
1.0.270 195 1/8/2026
1.0.269 207 1/7/2026
1.0.268 228 1/6/2026
1.0.267 148 1/6/2026
1.0.266 294 1/4/2026
1.0.265 240 1/1/2026
1.0.264 179 12/31/2025
1.0.263 193 12/30/2025
1.0.262 176 12/30/2025
1.0.261 174 12/29/2025
1.0.260 180 12/29/2025
1.0.259 191 12/26/2025
1.0.258 176 12/26/2025
1.0.257 182 12/26/2025
1.0.256 191 12/26/2025
1.0.255 270 12/25/2025
1.0.254 269 12/25/2025
1.0.253 274 12/24/2025
1.0.252 278 12/22/2025
1.0.251 256 12/21/2025
1.0.250 337 12/19/2025
1.0.249 334 12/19/2025
1.0.248 434 12/17/2025
1.0.247 442 12/16/2025
1.0.246 358 12/15/2025
1.0.245 346 12/15/2025
1.0.244 316 12/14/2025
1.0.243 262 12/14/2025
1.0.242 277 12/13/2025
1.0.241 269 12/13/2025
1.0.240 260 12/12/2025
1.0.239 223 12/12/2025
1.0.238 233 12/12/2025
1.0.237 224 12/12/2025
1.0.236 222 12/12/2025
1.0.235 230 12/12/2025
1.0.234 227 12/12/2025
1.0.233 914 12/2/2025
1.0.232 792 12/2/2025
1.0.231 785 12/2/2025
1.0.230 412 11/30/2025
1.0.229 258 11/29/2025
1.0.228 254 11/28/2025
1.0.227 259 11/28/2025
1.0.226 326 11/27/2025
1.0.225 415 11/24/2025
1.0.224 334 11/24/2025
1.0.223 346 11/23/2025
1.0.222 318 11/23/2025
1.0.221 395 11/22/2025
1.0.220 627 11/20/2025
1.0.219 577 11/18/2025
1.0.218 533 11/18/2025
1.0.217 529 11/13/2025
1.0.216 425 11/10/2025
1.0.215 1,303 11/1/2025
1.0.214 404 10/28/2025
1.0.213 413 10/27/2025
1.0.212 359 10/27/2025
1.0.211 289 10/25/2025
1.0.210 333 10/24/2025
1.0.209 532 10/20/2025
1.0.208 554 10/12/2025
1.0.207 327 10/11/2025
1.0.206 616 10/7/2025
1.0.205 430 10/6/2025
1.0.204 651 10/3/2025
1.0.203 454 10/1/2025
1.0.202 362 10/1/2025
1.0.201 386 9/30/2025
1.0.200 411 9/28/2025
1.0.199 449 9/25/2025
1.0.198 3,108 9/5/2025
1.0.197 461 9/2/2025
1.0.196 1,181 8/30/2025
1.0.195 444 8/30/2025
1.0.194 480 8/20/2025
1.0.193 366 8/20/2025
1.0.192 413 8/19/2025
1.0.191 427 8/15/2025
1.0.190 2,560 7/16/2025
1.0.189 843 7/14/2025
1.0.188 448 7/13/2025
1.0.187 382 7/13/2025
1.0.186 371 7/12/2025
1.0.185 706 7/8/2025
1.0.184 680 7/4/2025
1.0.183 424 7/2/2025
1.0.182 832 6/24/2025
1.0.181 1,453 6/16/2025
1.0.180 576 6/9/2025
1.0.179 454 6/8/2025
1.0.178 847 5/25/2025
1.0.177 418 5/21/2025
1.0.176 401 5/21/2025
1.0.175 365 5/17/2025
1.0.174 360 5/17/2025
1.0.173 370 5/17/2025
1.0.172 902 5/12/2025
1.0.171 492 5/12/2025
1.0.170 450 5/12/2025
1.0.169 396 5/11/2025
1.0.168 385 5/11/2025
1.0.167 357 5/10/2025
1.0.166 346 5/10/2025
1.0.165 368 5/3/2025
1.0.164 527 4/17/2025
1.0.163 489 4/15/2025
1.0.162 410 4/12/2025
1.0.161 445 4/9/2025
1.0.160 1,667 3/22/2025
1.0.159 426 3/20/2025
1.0.158 403 3/20/2025
1.0.157 408 3/19/2025
1.0.156 1,371 2/26/2025
1.0.155 385 2/26/2025
1.0.154 1,602 2/8/2025
1.0.153 384 2/8/2025
1.0.152 374 2/8/2025
1.0.151 391 2/6/2025
1.0.150 387 2/6/2025
1.0.149 393 2/6/2025
1.0.148 379 2/6/2025
1.0.147 370 2/6/2025
1.0.146 409 2/5/2025
1.0.145 406 2/5/2025
1.0.144 401 2/5/2025
1.0.143 427 2/3/2025
1.0.142 384 2/2/2025
1.0.141 396 2/1/2025
1.0.140 406 1/31/2025
1.0.139 400 1/30/2025
1.0.138 413 1/26/2025
1.0.137 500 1/21/2025
1.0.136 380 1/21/2025
1.0.135 420 1/20/2025
1.0.134 380 1/20/2025
1.0.133 473 1/19/2025
1.0.132 423 1/19/2025
1.0.131 530 1/17/2025
1.0.130 405 1/17/2025
1.0.129 455 1/14/2025
1.0.128 681 1/12/2025
1.0.127 472 1/12/2025
1.0.126 471 1/12/2025
1.0.125 487 1/12/2025
1.0.124 471 1/12/2025
1.0.123 441 1/12/2025
1.0.122 448 1/12/2025
1.0.121 465 1/11/2025
1.0.120 496 1/10/2025
1.0.119 503 1/10/2025
1.0.118 499 1/9/2025
1.0.117 430 1/9/2025
1.0.116 403 1/9/2025
1.0.115 428 1/9/2025
1.0.114 1,635 12/27/2024
1.0.113 467 12/19/2024
1.0.112 491 11/20/2024
1.0.111 1,314 11/18/2024
1.0.110 710 11/7/2024
1.0.109 353 11/7/2024
1.0.108 382 11/7/2024
1.0.107 356 11/7/2024
1.0.106 356 11/7/2024
1.0.105 355 11/7/2024
1.0.104 355 11/7/2024
1.0.103 379 11/6/2024
1.0.102 392 10/31/2024
1.0.101 474 10/19/2024
1.0.100 1,077 10/12/2024
1.0.99 715 10/9/2024
1.0.98 640 10/5/2024
1.0.97 1,346 9/18/2024
1.0.96 407 9/17/2024
1.0.95 1,083 9/3/2024
1.0.94 391 9/1/2024
1.0.93 1,567 8/8/2024
1.0.92 2,617 6/12/2024
1.0.91 852 5/28/2024
1.0.90 1,483 5/4/2024
1.0.89 1,076 4/23/2024
1.0.88 388 4/21/2024
1.0.87 446 4/14/2024
1.0.86 1,261 3/28/2024
1.0.85 456 3/17/2024
1.0.84 1,450 2/23/2024
1.0.83 429 2/23/2024
1.0.82 1,175 2/18/2024
1.0.81 368 2/18/2024
1.0.80 379 2/16/2024
1.0.79 1,013 2/13/2024
1.0.78 844 2/8/2024
1.0.77 1,128 2/5/2024
1.0.76 390 2/4/2024
1.0.75 1,133 1/23/2024
1.0.74 379 1/23/2024
1.0.73 1,084 1/12/2024
1.0.72 1,205 1/2/2024
1.0.71 433 12/29/2023
1.0.70 1,405 12/15/2023
1.0.69 494 12/15/2023
1.0.68 460 12/13/2023
1.0.67 482 12/13/2023
1.0.66 2,431 11/12/2023
1.0.65 477 11/10/2023
1.0.64 460 11/10/2023
1.0.63 456 11/9/2023
1.0.62 468 11/3/2023
1.0.61 475 11/1/2023
1.0.60 488 11/1/2023
1.0.59 4,183 9/8/2023
1.0.58 434 9/8/2023
1.0.57 443 9/8/2023
1.0.56 508 9/8/2023
1.0.55 469 9/8/2023
1.0.54 463 9/7/2023
1.0.53 573 9/3/2023
1.0.52 621 8/21/2023
1.0.51 605 8/15/2023
1.0.50 538 8/14/2023
1.0.49 524 8/14/2023
1.0.48 539 8/10/2023
1.0.47 3,887 7/1/2023
1.0.46 549 6/29/2023
1.0.45 2,132 5/27/2023
1.0.44 610 5/21/2023
1.0.43 635 5/19/2023
1.0.42 2,451 5/8/2023
1.0.41 659 5/1/2023
1.0.40 642 4/22/2023
1.0.39 642 4/21/2023
1.0.38 4,318 4/3/2023
1.0.37 761 3/27/2023
1.0.36 817 3/21/2023
1.0.35 794 3/13/2023
1.0.34 673 3/12/2023
1.0.33 718 3/11/2023
1.0.32 2,249 3/6/2023
1.0.31 795 2/26/2023
1.0.30 2,595 2/21/2023
1.0.29 787 2/20/2023
1.0.28 813 2/15/2023
1.0.27 819 2/14/2023
1.0.26 3,345 2/9/2023
1.0.25 2,443 2/7/2023
1.0.24 855 2/4/2023
1.0.23 2,595 2/2/2023
1.0.22 2,642 1/30/2023
1.0.21 899 1/18/2023
1.0.20 5,221 12/30/2022
1.0.19 908 12/23/2022
1.0.18 875 12/23/2022
1.0.17 22,978 12/12/2022
1.0.16 66,357 11/11/2022
1.0.15 2,715 11/11/2022
1.0.14 5,130 11/5/2022
1.0.13 29,201 11/1/2022
1.0.12 26,921 10/16/2022
1.0.11 58,872 9/8/2022
1.0.10 3,050 9/8/2022
1.0.9 2,998 9/8/2022
1.0.8 99,294 8/24/2022
1.0.7 17,211 7/26/2022
1.0.6 103,246 7/18/2022
1.0.5 111,295 4/30/2022
1.0.4 23,859 3/29/2022
1.0.3 462,559 12/29/2021
1.0.2 31,414 12/20/2021
1.0.1 63,372 12/6/2021
1.0.0 4,338 12/2/2021