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 0 1/26/2026
1.0.286 0 1/26/2026
1.0.285 53 1/22/2026
1.0.284 58 1/22/2026
1.0.283 146 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 190 1/13/2026
1.0.275 194 1/13/2026
1.0.274 197 1/12/2026
1.0.273 287 1/9/2026
1.0.272 233 1/9/2026
1.0.271 188 1/8/2026
1.0.270 193 1/8/2026
1.0.269 206 1/7/2026
1.0.268 219 1/6/2026
1.0.267 146 1/6/2026
1.0.266 293 1/4/2026
1.0.265 239 1/1/2026
1.0.264 178 12/31/2025
1.0.263 192 12/30/2025
1.0.262 175 12/30/2025
1.0.261 172 12/29/2025
1.0.260 179 12/29/2025
1.0.259 184 12/26/2025
1.0.258 168 12/26/2025
1.0.257 175 12/26/2025
1.0.256 185 12/26/2025
1.0.255 263 12/25/2025
1.0.254 261 12/25/2025
1.0.253 267 12/24/2025
1.0.252 270 12/22/2025
1.0.251 248 12/21/2025
1.0.250 331 12/19/2025
1.0.249 327 12/19/2025
1.0.248 428 12/17/2025
1.0.247 433 12/16/2025
1.0.246 352 12/15/2025
1.0.245 340 12/15/2025
1.0.244 310 12/14/2025
1.0.243 253 12/14/2025
1.0.242 271 12/13/2025
1.0.241 262 12/13/2025
1.0.240 254 12/12/2025
1.0.239 216 12/12/2025
1.0.238 224 12/12/2025
1.0.237 218 12/12/2025
1.0.236 216 12/12/2025
1.0.235 223 12/12/2025
1.0.234 219 12/12/2025
1.0.233 907 12/2/2025
1.0.232 786 12/2/2025
1.0.231 779 12/2/2025
1.0.230 405 11/30/2025
1.0.229 249 11/29/2025
1.0.228 248 11/28/2025
1.0.227 252 11/28/2025
1.0.226 320 11/27/2025
1.0.225 407 11/24/2025
1.0.224 327 11/24/2025
1.0.223 339 11/23/2025
1.0.222 311 11/23/2025
1.0.221 387 11/22/2025
1.0.220 621 11/20/2025
1.0.219 570 11/18/2025
1.0.218 527 11/18/2025
1.0.217 522 11/13/2025
1.0.216 419 11/10/2025
1.0.215 1,296 11/1/2025
1.0.214 398 10/28/2025
1.0.213 405 10/27/2025
1.0.212 351 10/27/2025
1.0.211 282 10/25/2025
1.0.210 326 10/24/2025
1.0.209 526 10/20/2025
1.0.208 548 10/12/2025
1.0.207 319 10/11/2025
1.0.206 609 10/7/2025
1.0.205 424 10/6/2025
1.0.204 643 10/3/2025
1.0.203 448 10/1/2025
1.0.202 356 10/1/2025
1.0.201 378 9/30/2025
1.0.200 403 9/28/2025
1.0.199 441 9/25/2025
1.0.198 3,089 9/5/2025
1.0.197 455 9/2/2025
1.0.196 1,175 8/30/2025
1.0.195 437 8/30/2025
1.0.194 473 8/20/2025
1.0.193 360 8/20/2025
1.0.192 406 8/19/2025
1.0.191 419 8/15/2025
1.0.190 2,552 7/16/2025
1.0.189 835 7/14/2025
1.0.188 442 7/13/2025
1.0.187 376 7/13/2025
1.0.186 364 7/12/2025
1.0.185 700 7/8/2025
1.0.184 673 7/4/2025
1.0.183 415 7/2/2025
1.0.182 826 6/24/2025
1.0.181 1,447 6/16/2025
1.0.180 569 6/9/2025
1.0.179 447 6/8/2025
1.0.178 839 5/25/2025
1.0.177 412 5/21/2025
1.0.176 393 5/21/2025
1.0.175 358 5/17/2025
1.0.174 354 5/17/2025
1.0.173 364 5/17/2025
1.0.172 894 5/12/2025
1.0.171 486 5/12/2025
1.0.170 443 5/12/2025
1.0.169 390 5/11/2025
1.0.168 376 5/11/2025
1.0.167 350 5/10/2025
1.0.166 339 5/10/2025
1.0.165 361 5/3/2025
1.0.164 520 4/17/2025
1.0.163 481 4/15/2025
1.0.162 404 4/12/2025
1.0.161 439 4/9/2025
1.0.160 1,659 3/22/2025
1.0.159 420 3/20/2025
1.0.158 397 3/20/2025
1.0.157 402 3/19/2025
1.0.156 1,362 2/26/2025
1.0.155 378 2/26/2025
1.0.154 1,594 2/8/2025
1.0.153 376 2/8/2025
1.0.152 367 2/8/2025
1.0.151 382 2/6/2025
1.0.150 380 2/6/2025
1.0.149 385 2/6/2025
1.0.148 373 2/6/2025
1.0.147 364 2/6/2025
1.0.146 403 2/5/2025
1.0.145 399 2/5/2025
1.0.144 393 2/5/2025
1.0.143 420 2/3/2025
1.0.142 377 2/2/2025
1.0.141 390 2/1/2025
1.0.140 399 1/31/2025
1.0.139 393 1/30/2025
1.0.138 406 1/26/2025
1.0.137 492 1/21/2025
1.0.136 374 1/21/2025
1.0.135 413 1/20/2025
1.0.134 372 1/20/2025
1.0.133 466 1/19/2025
1.0.132 414 1/19/2025
1.0.131 523 1/17/2025
1.0.130 397 1/17/2025
1.0.129 448 1/14/2025
1.0.128 675 1/12/2025
1.0.127 464 1/12/2025
1.0.126 465 1/12/2025
1.0.125 481 1/12/2025
1.0.124 463 1/12/2025
1.0.123 435 1/12/2025
1.0.122 442 1/12/2025
1.0.121 459 1/11/2025
1.0.120 489 1/10/2025
1.0.119 497 1/10/2025
1.0.118 491 1/9/2025
1.0.117 423 1/9/2025
1.0.116 397 1/9/2025
1.0.115 422 1/9/2025
1.0.114 1,629 12/27/2024
1.0.113 460 12/19/2024
1.0.112 485 11/20/2024
1.0.111 1,306 11/18/2024
1.0.110 703 11/7/2024
1.0.109 346 11/7/2024
1.0.108 375 11/7/2024
1.0.107 349 11/7/2024
1.0.106 350 11/7/2024
1.0.105 348 11/7/2024
1.0.104 349 11/7/2024
1.0.103 372 11/6/2024
1.0.102 386 10/31/2024
1.0.101 467 10/19/2024
1.0.100 1,068 10/12/2024
1.0.99 708 10/9/2024
1.0.98 633 10/5/2024
1.0.97 1,340 9/18/2024
1.0.96 399 9/17/2024
1.0.95 1,076 9/3/2024
1.0.94 384 9/1/2024
1.0.93 1,561 8/8/2024
1.0.92 2,608 6/12/2024
1.0.91 844 5/28/2024
1.0.90 1,476 5/4/2024
1.0.89 1,069 4/23/2024
1.0.88 381 4/21/2024
1.0.87 437 4/14/2024
1.0.86 1,255 3/28/2024
1.0.85 450 3/17/2024
1.0.84 1,444 2/23/2024
1.0.83 421 2/23/2024
1.0.82 1,169 2/18/2024
1.0.81 362 2/18/2024
1.0.80 372 2/16/2024
1.0.79 1,007 2/13/2024
1.0.78 838 2/8/2024
1.0.77 1,121 2/5/2024
1.0.76 383 2/4/2024
1.0.75 1,126 1/23/2024
1.0.74 372 1/23/2024
1.0.73 1,078 1/12/2024
1.0.72 1,197 1/2/2024
1.0.71 427 12/29/2023
1.0.70 1,397 12/15/2023
1.0.69 487 12/15/2023
1.0.68 453 12/13/2023
1.0.67 475 12/13/2023
1.0.66 2,422 11/12/2023
1.0.65 471 11/10/2023
1.0.64 452 11/10/2023
1.0.63 449 11/9/2023
1.0.62 461 11/3/2023
1.0.61 469 11/1/2023
1.0.60 481 11/1/2023
1.0.59 4,177 9/8/2023
1.0.58 427 9/8/2023
1.0.57 437 9/8/2023
1.0.56 502 9/8/2023
1.0.55 462 9/8/2023
1.0.54 456 9/7/2023
1.0.53 564 9/3/2023
1.0.52 615 8/21/2023
1.0.51 599 8/15/2023
1.0.50 531 8/14/2023
1.0.49 516 8/14/2023
1.0.48 533 8/10/2023
1.0.47 3,879 7/1/2023
1.0.46 543 6/29/2023
1.0.45 2,126 5/27/2023
1.0.44 604 5/21/2023
1.0.43 629 5/19/2023
1.0.42 2,444 5/8/2023
1.0.41 651 5/1/2023
1.0.40 635 4/22/2023
1.0.39 634 4/21/2023
1.0.38 4,312 4/3/2023
1.0.37 752 3/27/2023
1.0.36 811 3/21/2023
1.0.35 787 3/13/2023
1.0.34 667 3/12/2023
1.0.33 711 3/11/2023
1.0.32 2,243 3/6/2023
1.0.31 788 2/26/2023
1.0.30 2,588 2/21/2023
1.0.29 781 2/20/2023
1.0.28 806 2/15/2023
1.0.27 813 2/14/2023
1.0.26 3,338 2/9/2023
1.0.25 2,437 2/7/2023
1.0.24 848 2/4/2023
1.0.23 2,587 2/2/2023
1.0.22 2,636 1/30/2023
1.0.21 893 1/18/2023
1.0.20 5,215 12/30/2022
1.0.19 902 12/23/2022
1.0.18 867 12/23/2022
1.0.17 22,969 12/12/2022
1.0.16 66,351 11/11/2022
1.0.15 2,708 11/11/2022
1.0.14 5,124 11/5/2022
1.0.13 29,195 11/1/2022
1.0.12 26,914 10/16/2022
1.0.11 58,865 9/8/2022
1.0.10 3,044 9/8/2022
1.0.9 2,991 9/8/2022
1.0.8 99,287 8/24/2022
1.0.7 17,204 7/26/2022
1.0.6 103,240 7/18/2022
1.0.5 111,288 4/30/2022
1.0.4 23,853 3/29/2022
1.0.3 462,529 12/29/2021
1.0.2 31,408 12/20/2021
1.0.1 63,366 12/6/2021
1.0.0 4,332 12/2/2021