Ecng.Compilation 1.0.270

There is a newer version of this package available.
See the version list below for details.
dotnet add package Ecng.Compilation --version 1.0.270
                    
NuGet\Install-Package Ecng.Compilation -Version 1.0.270
                    
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.270" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Ecng.Compilation" Version="1.0.270" />
                    
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.270
                    
#r "nuget: Ecng.Compilation, 1.0.270"
                    
#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.270
                    
#: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.270
                    
Install as a Cake Addin
#tool nuget:?package=Ecng.Compilation&version=1.0.270
                    
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 69 1/9/2026
1.0.272 78 1/9/2026
1.0.271 37 1/8/2026
1.0.270 43 1/8/2026
1.0.269 104 1/7/2026
1.0.268 165 1/6/2026
1.0.267 127 1/6/2026
1.0.266 259 1/4/2026
1.0.265 226 1/1/2026
1.0.264 167 12/31/2025
1.0.263 180 12/30/2025
1.0.262 166 12/30/2025
1.0.261 162 12/29/2025
1.0.260 169 12/29/2025
1.0.259 174 12/26/2025
1.0.258 158 12/26/2025
1.0.257 165 12/26/2025
1.0.256 174 12/26/2025
1.0.255 254 12/25/2025
1.0.254 252 12/25/2025
1.0.253 255 12/24/2025
1.0.252 256 12/22/2025
1.0.251 236 12/21/2025
1.0.250 317 12/19/2025
1.0.249 315 12/19/2025
1.0.248 413 12/17/2025
1.0.247 414 12/16/2025
1.0.246 334 12/15/2025
1.0.245 321 12/15/2025
1.0.244 293 12/14/2025
1.0.243 235 12/14/2025
1.0.242 244 12/13/2025
1.0.241 236 12/13/2025
1.0.240 229 12/12/2025
1.0.239 192 12/12/2025
1.0.238 198 12/12/2025
1.0.237 191 12/12/2025
1.0.236 191 12/12/2025
1.0.235 194 12/12/2025
1.0.234 190 12/12/2025
1.0.233 880 12/2/2025
1.0.232 762 12/2/2025
1.0.231 755 12/2/2025
1.0.230 380 11/30/2025
1.0.229 222 11/29/2025
1.0.228 223 11/28/2025
1.0.227 227 11/28/2025
1.0.226 290 11/27/2025
1.0.225 382 11/24/2025
1.0.224 301 11/24/2025
1.0.223 308 11/23/2025
1.0.222 282 11/23/2025
1.0.221 357 11/22/2025
1.0.220 589 11/20/2025
1.0.219 544 11/18/2025
1.0.218 502 11/18/2025
1.0.217 497 11/13/2025
1.0.216 390 11/10/2025
1.0.215 1,270 11/1/2025
1.0.214 370 10/28/2025
1.0.213 377 10/27/2025
1.0.212 325 10/27/2025
1.0.211 255 10/25/2025
1.0.210 299 10/24/2025
1.0.209 500 10/20/2025
1.0.208 521 10/12/2025
1.0.207 289 10/11/2025
1.0.206 581 10/7/2025
1.0.205 396 10/6/2025
1.0.204 614 10/3/2025
1.0.203 417 10/1/2025
1.0.202 327 10/1/2025
1.0.201 352 9/30/2025
1.0.200 378 9/28/2025
1.0.199 413 9/25/2025
1.0.198 2,976 9/5/2025
1.0.197 429 9/2/2025
1.0.196 1,149 8/30/2025
1.0.195 405 8/30/2025
1.0.194 440 8/20/2025
1.0.193 332 8/20/2025
1.0.192 378 8/19/2025
1.0.191 392 8/15/2025
1.0.190 2,520 7/16/2025
1.0.189 798 7/14/2025
1.0.188 407 7/13/2025
1.0.187 348 7/13/2025
1.0.186 337 7/12/2025
1.0.185 669 7/8/2025
1.0.184 645 7/4/2025
1.0.183 389 7/2/2025
1.0.182 799 6/24/2025
1.0.181 1,419 6/16/2025
1.0.180 539 6/9/2025
1.0.179 422 6/8/2025
1.0.178 810 5/25/2025
1.0.177 388 5/21/2025
1.0.176 368 5/21/2025
1.0.175 330 5/17/2025
1.0.174 327 5/17/2025
1.0.173 337 5/17/2025
1.0.172 868 5/12/2025
1.0.171 460 5/12/2025
1.0.170 414 5/12/2025
1.0.169 360 5/11/2025
1.0.168 348 5/11/2025
1.0.167 322 5/10/2025
1.0.166 310 5/10/2025
1.0.165 333 5/3/2025
1.0.164 490 4/17/2025
1.0.163 454 4/15/2025
1.0.162 378 4/12/2025
1.0.161 409 4/9/2025
1.0.160 1,631 3/22/2025
1.0.159 394 3/20/2025
1.0.158 370 3/20/2025
1.0.157 373 3/19/2025
1.0.156 1,333 2/26/2025
1.0.155 355 2/26/2025
1.0.154 1,569 2/8/2025
1.0.153 344 2/8/2025
1.0.152 340 2/8/2025
1.0.151 354 2/6/2025
1.0.150 345 2/6/2025
1.0.149 358 2/6/2025
1.0.148 347 2/6/2025
1.0.147 338 2/6/2025
1.0.146 375 2/5/2025
1.0.145 368 2/5/2025
1.0.144 368 2/5/2025
1.0.143 396 2/3/2025
1.0.142 350 2/2/2025
1.0.141 362 2/1/2025
1.0.140 372 1/31/2025
1.0.139 362 1/30/2025
1.0.138 376 1/26/2025
1.0.137 467 1/21/2025
1.0.136 348 1/21/2025
1.0.135 384 1/20/2025
1.0.134 343 1/20/2025
1.0.133 442 1/19/2025
1.0.132 386 1/19/2025
1.0.131 496 1/17/2025
1.0.130 370 1/17/2025
1.0.129 422 1/14/2025
1.0.128 649 1/12/2025
1.0.127 435 1/12/2025
1.0.126 440 1/12/2025
1.0.125 454 1/12/2025
1.0.124 432 1/12/2025
1.0.123 405 1/12/2025
1.0.122 413 1/12/2025
1.0.121 430 1/11/2025
1.0.120 461 1/10/2025
1.0.119 469 1/10/2025
1.0.118 465 1/9/2025
1.0.117 394 1/9/2025
1.0.116 371 1/9/2025
1.0.115 395 1/9/2025
1.0.114 1,602 12/27/2024
1.0.113 432 12/19/2024
1.0.112 454 11/20/2024
1.0.111 1,280 11/18/2024
1.0.110 677 11/7/2024
1.0.109 321 11/7/2024
1.0.108 349 11/7/2024
1.0.107 321 11/7/2024
1.0.106 323 11/7/2024
1.0.105 319 11/7/2024
1.0.104 325 11/7/2024
1.0.103 345 11/6/2024
1.0.102 356 10/31/2024
1.0.101 439 10/19/2024
1.0.100 1,037 10/12/2024
1.0.99 683 10/9/2024
1.0.98 607 10/5/2024
1.0.97 1,311 9/18/2024
1.0.96 372 9/17/2024
1.0.95 1,051 9/3/2024
1.0.94 356 9/1/2024
1.0.93 1,534 8/8/2024
1.0.92 2,581 6/12/2024
1.0.91 817 5/28/2024
1.0.90 1,449 5/4/2024
1.0.89 1,044 4/23/2024
1.0.88 352 4/21/2024
1.0.87 411 4/14/2024
1.0.86 1,226 3/28/2024
1.0.85 420 3/17/2024
1.0.84 1,419 2/23/2024
1.0.83 395 2/23/2024
1.0.82 1,141 2/18/2024
1.0.81 335 2/18/2024
1.0.80 341 2/16/2024
1.0.79 980 2/13/2024
1.0.78 812 2/8/2024
1.0.77 1,094 2/5/2024
1.0.76 355 2/4/2024
1.0.75 1,096 1/23/2024
1.0.74 343 1/23/2024
1.0.73 1,054 1/12/2024
1.0.72 1,173 1/2/2024
1.0.71 398 12/29/2023
1.0.70 1,372 12/15/2023
1.0.69 462 12/15/2023
1.0.68 422 12/13/2023
1.0.67 450 12/13/2023
1.0.66 2,403 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,157 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 442 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 498 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,104 5/27/2023
1.0.44 584 5/21/2023
1.0.43 608 5/19/2023
1.0.42 2,424 5/8/2023
1.0.41 633 5/1/2023
1.0.40 617 4/22/2023
1.0.39 615 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,220 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 792 2/14/2023
1.0.26 3,320 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,617 1/30/2023
1.0.21 869 1/18/2023
1.0.20 5,194 12/30/2022
1.0.19 885 12/23/2022
1.0.18 844 12/23/2022
1.0.17 22,947 12/12/2022
1.0.16 66,328 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,027 9/8/2022
1.0.9 2,971 9/8/2022
1.0.8 99,269 8/24/2022
1.0.7 17,182 7/26/2022
1.0.6 103,220 7/18/2022
1.0.5 111,270 4/30/2022
1.0.4 23,835 3/29/2022
1.0.3 462,463 12/29/2021
1.0.2 31,387 12/20/2021
1.0.1 63,344 12/6/2021
1.0.0 4,312 12/2/2021