Ecng.Compilation 1.0.273

dotnet add package Ecng.Compilation --version 1.0.273
                    
NuGet\Install-Package Ecng.Compilation -Version 1.0.273
                    
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.273" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Ecng.Compilation" Version="1.0.273" />
                    
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.273
                    
#r "nuget: Ecng.Compilation, 1.0.273"
                    
#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.273
                    
#: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.273
                    
Install as a Cake Addin
#tool nuget:?package=Ecng.Compilation&version=1.0.273
                    
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.273 94 1/9/2026
1.0.272 81 1/9/2026
1.0.271 40 1/8/2026
1.0.270 46 1/8/2026
1.0.269 111 1/7/2026
1.0.268 167 1/6/2026
1.0.267 130 1/6/2026
1.0.266 261 1/4/2026
1.0.265 230 1/1/2026
1.0.264 170 12/31/2025
1.0.263 183 12/30/2025
1.0.262 168 12/30/2025
1.0.261 164 12/29/2025
1.0.260 171 12/29/2025
1.0.259 176 12/26/2025
1.0.258 160 12/26/2025
1.0.257 167 12/26/2025
1.0.256 176 12/26/2025
1.0.255 256 12/25/2025
1.0.254 254 12/25/2025
1.0.253 257 12/24/2025
1.0.252 258 12/22/2025
1.0.251 238 12/21/2025
1.0.250 321 12/19/2025
1.0.249 317 12/19/2025
1.0.248 415 12/17/2025
1.0.247 416 12/16/2025
1.0.246 336 12/15/2025
1.0.245 323 12/15/2025
1.0.244 295 12/14/2025
1.0.243 237 12/14/2025
1.0.242 247 12/13/2025
1.0.241 238 12/13/2025
1.0.240 232 12/12/2025
1.0.239 194 12/12/2025
1.0.238 201 12/12/2025
1.0.237 193 12/12/2025
1.0.236 194 12/12/2025
1.0.235 197 12/12/2025
1.0.234 192 12/12/2025
1.0.233 882 12/2/2025
1.0.232 764 12/2/2025
1.0.231 757 12/2/2025
1.0.230 382 11/30/2025
1.0.229 224 11/29/2025
1.0.228 225 11/28/2025
1.0.227 230 11/28/2025
1.0.226 292 11/27/2025
1.0.225 385 11/24/2025
1.0.224 303 11/24/2025
1.0.223 311 11/23/2025
1.0.222 284 11/23/2025
1.0.221 359 11/22/2025
1.0.220 591 11/20/2025
1.0.219 546 11/18/2025
1.0.218 504 11/18/2025
1.0.217 499 11/13/2025
1.0.216 392 11/10/2025
1.0.215 1,273 11/1/2025
1.0.214 372 10/28/2025
1.0.213 380 10/27/2025
1.0.212 327 10/27/2025
1.0.211 257 10/25/2025
1.0.210 301 10/24/2025
1.0.209 502 10/20/2025
1.0.208 524 10/12/2025
1.0.207 291 10/11/2025
1.0.206 583 10/7/2025
1.0.205 398 10/6/2025
1.0.204 616 10/3/2025
1.0.203 419 10/1/2025
1.0.202 329 10/1/2025
1.0.201 354 9/30/2025
1.0.200 380 9/28/2025
1.0.199 415 9/25/2025
1.0.198 2,983 9/5/2025
1.0.197 431 9/2/2025
1.0.196 1,152 8/30/2025
1.0.195 408 8/30/2025
1.0.194 443 8/20/2025
1.0.193 334 8/20/2025
1.0.192 381 8/19/2025
1.0.191 394 8/15/2025
1.0.190 2,522 7/16/2025
1.0.189 800 7/14/2025
1.0.188 411 7/13/2025
1.0.187 350 7/13/2025
1.0.186 339 7/12/2025
1.0.185 671 7/8/2025
1.0.184 647 7/4/2025
1.0.183 391 7/2/2025
1.0.182 802 6/24/2025
1.0.181 1,421 6/16/2025
1.0.180 542 6/9/2025
1.0.179 424 6/8/2025
1.0.178 813 5/25/2025
1.0.177 390 5/21/2025
1.0.176 370 5/21/2025
1.0.175 333 5/17/2025
1.0.174 329 5/17/2025
1.0.173 339 5/17/2025
1.0.172 870 5/12/2025
1.0.171 462 5/12/2025
1.0.170 417 5/12/2025
1.0.169 362 5/11/2025
1.0.168 350 5/11/2025
1.0.167 324 5/10/2025
1.0.166 312 5/10/2025
1.0.165 335 5/3/2025
1.0.164 493 4/17/2025
1.0.163 457 4/15/2025
1.0.162 380 4/12/2025
1.0.161 412 4/9/2025
1.0.160 1,633 3/22/2025
1.0.159 396 3/20/2025
1.0.158 372 3/20/2025
1.0.157 375 3/19/2025
1.0.156 1,336 2/26/2025
1.0.155 357 2/26/2025
1.0.154 1,571 2/8/2025
1.0.153 346 2/8/2025
1.0.152 342 2/8/2025
1.0.151 357 2/6/2025
1.0.150 353 2/6/2025
1.0.149 360 2/6/2025
1.0.148 349 2/6/2025
1.0.147 341 2/6/2025
1.0.146 378 2/5/2025
1.0.145 371 2/5/2025
1.0.144 370 2/5/2025
1.0.143 398 2/3/2025
1.0.142 352 2/2/2025
1.0.141 364 2/1/2025
1.0.140 375 1/31/2025
1.0.139 365 1/30/2025
1.0.138 379 1/26/2025
1.0.137 469 1/21/2025
1.0.136 350 1/21/2025
1.0.135 386 1/20/2025
1.0.134 346 1/20/2025
1.0.133 444 1/19/2025
1.0.132 388 1/19/2025
1.0.131 498 1/17/2025
1.0.130 372 1/17/2025
1.0.129 424 1/14/2025
1.0.128 652 1/12/2025
1.0.127 439 1/12/2025
1.0.126 442 1/12/2025
1.0.125 456 1/12/2025
1.0.124 434 1/12/2025
1.0.123 407 1/12/2025
1.0.122 415 1/12/2025
1.0.121 432 1/11/2025
1.0.120 465 1/10/2025
1.0.119 471 1/10/2025
1.0.118 467 1/9/2025
1.0.117 397 1/9/2025
1.0.116 373 1/9/2025
1.0.115 398 1/9/2025
1.0.114 1,604 12/27/2024
1.0.113 434 12/19/2024
1.0.112 457 11/20/2024
1.0.111 1,282 11/18/2024
1.0.110 679 11/7/2024
1.0.109 323 11/7/2024
1.0.108 353 11/7/2024
1.0.107 323 11/7/2024
1.0.106 326 11/7/2024
1.0.105 321 11/7/2024
1.0.104 327 11/7/2024
1.0.103 348 11/6/2024
1.0.102 358 10/31/2024
1.0.101 441 10/19/2024
1.0.100 1,040 10/12/2024
1.0.99 685 10/9/2024
1.0.98 609 10/5/2024
1.0.97 1,314 9/18/2024
1.0.96 375 9/17/2024
1.0.95 1,053 9/3/2024
1.0.94 359 9/1/2024
1.0.93 1,536 8/8/2024
1.0.92 2,583 6/12/2024
1.0.91 819 5/28/2024
1.0.90 1,452 5/4/2024
1.0.89 1,046 4/23/2024
1.0.88 355 4/21/2024
1.0.87 413 4/14/2024
1.0.86 1,229 3/28/2024
1.0.85 422 3/17/2024
1.0.84 1,421 2/23/2024
1.0.83 397 2/23/2024
1.0.82 1,143 2/18/2024
1.0.81 337 2/18/2024
1.0.80 344 2/16/2024
1.0.79 982 2/13/2024
1.0.78 815 2/8/2024
1.0.77 1,096 2/5/2024
1.0.76 357 2/4/2024
1.0.75 1,099 1/23/2024
1.0.74 346 1/23/2024
1.0.73 1,056 1/12/2024
1.0.72 1,176 1/2/2024
1.0.71 400 12/29/2023
1.0.70 1,374 12/15/2023
1.0.69 464 12/15/2023
1.0.68 425 12/13/2023
1.0.67 452 12/13/2023
1.0.66 2,405 11/12/2023
1.0.65 455 11/10/2023
1.0.64 434 11/10/2023
1.0.63 430 11/9/2023
1.0.62 443 11/3/2023
1.0.61 450 11/1/2023
1.0.60 457 11/1/2023
1.0.59 4,158 9/8/2023
1.0.58 408 9/8/2023
1.0.57 419 9/8/2023
1.0.56 483 9/8/2023
1.0.55 444 9/8/2023
1.0.54 437 9/7/2023
1.0.53 544 9/3/2023
1.0.52 593 8/21/2023
1.0.51 579 8/15/2023
1.0.50 516 8/14/2023
1.0.49 499 8/14/2023
1.0.48 512 8/10/2023
1.0.47 3,861 7/1/2023
1.0.46 523 6/29/2023
1.0.45 2,105 5/27/2023
1.0.44 585 5/21/2023
1.0.43 608 5/19/2023
1.0.42 2,426 5/8/2023
1.0.41 634 5/1/2023
1.0.40 617 4/22/2023
1.0.39 617 4/21/2023
1.0.38 4,295 4/3/2023
1.0.37 735 3/27/2023
1.0.36 789 3/21/2023
1.0.35 765 3/13/2023
1.0.34 646 3/12/2023
1.0.33 692 3/11/2023
1.0.32 2,221 3/6/2023
1.0.31 766 2/26/2023
1.0.30 2,566 2/21/2023
1.0.29 759 2/20/2023
1.0.28 786 2/15/2023
1.0.27 794 2/14/2023
1.0.26 3,321 2/9/2023
1.0.25 2,418 2/7/2023
1.0.24 826 2/4/2023
1.0.23 2,568 2/2/2023
1.0.22 2,618 1/30/2023
1.0.21 871 1/18/2023
1.0.20 5,194 12/30/2022
1.0.19 885 12/23/2022
1.0.18 845 12/23/2022
1.0.17 22,948 12/12/2022
1.0.16 66,329 11/11/2022
1.0.15 2,686 11/11/2022
1.0.14 5,106 11/5/2022
1.0.13 29,177 11/1/2022
1.0.12 26,896 10/16/2022
1.0.11 58,846 9/8/2022
1.0.10 3,028 9/8/2022
1.0.9 2,972 9/8/2022
1.0.8 99,270 8/24/2022
1.0.7 17,182 7/26/2022
1.0.6 103,221 7/18/2022
1.0.5 111,271 4/30/2022
1.0.4 23,835 3/29/2022
1.0.3 462,463 12/29/2021
1.0.2 31,388 12/20/2021
1.0.1 63,345 12/6/2021
1.0.0 4,312 12/2/2021

ExpressionHelper: auto-add 'm' suffix to decimal literals