Hyperbee Expressions
Hyperbee.Expressions extends the .NET expression tree model with language constructs that the standard System.Linq.Expressions library does not support: asynchronous workflows, enumerable state machines, structured loops, resource disposal, string formatting, and dependency injection.
All custom expression types reduce to standard expression trees, so they work with any compiler that accepts LambdaExpression – including the System compiler, FastExpressionCompiler, and the included Hyperbee Expression Compiler.
Packages
Getting Started
dotnet add package Hyperbee.Expressions
All factory methods live in ExpressionExtensions. Import them with:
using static Hyperbee.Expressions.ExpressionExtensions;
Quick Example
using static System.Linq.Expressions.Expression;
using static Hyperbee.Expressions.ExpressionExtensions;
// Build an async expression that awaits two tasks sequentially
var result1 = Variable( typeof(int), "result1" );
var result2 = Variable( typeof(int), "result2" );
var expr = BlockAsync(
[result1, result2],
Assign( result1, Await( Call( typeof(MyService).GetMethod("GetValueAsync") ) ) ),
Assign( result2, Await( Call( typeof(MyService).GetMethod("GetOtherAsync"), result1 ) ) ),
result2
);
var lambda = Lambda<Func<Task<int>>>( expr );
var fn = lambda.Compile();
var value = await fn();
Expression Types
Async
| Type | Factory Method | Description |
|---|---|---|
AsyncBlockExpression | BlockAsync(...) | Async code block with generated state machine |
AwaitExpression | Await(...) | Await a task or awaitable |
Enumerable / Yield
| Type | Factory Method | Description |
|---|---|---|
EnumerableBlockExpression | BlockEnumerable(...) | Enumerable block with generated state machine |
YieldExpression | YieldReturn(...) / YieldBreak() | Yield a value or break from enumeration |
Loops
| Type | Factory Method | Description |
|---|---|---|
ForExpression | For(...) | for loop with init / test / iteration |
ForEachExpression | ForEach(...) | foreach loop over any IEnumerable |
WhileExpression | While(...) | while loop |
Resource Management
| Type | Factory Method | Description |
|---|---|---|
UsingExpression | Using(...) | using block for IDisposable |
Utilities
| Type | Factory Method | Description |
|---|---|---|
StringFormatExpression | StringFormat(...) | string.Format in an expression |
DebugExpression | Debug(...) | Debug callback for expression trees |
InjectExpression | Inject(...) | Resolve a service from IServiceProvider |
ConfigurationExpression | ConfigurationValue(...) | Read a value from IConfiguration |
Compiler
Hyperbee.Expressions.Compiler is a high-performance IR-based compiler that replaces Expression.Compile(). It emits IL directly without the overhead of the System expression interpreter.
dotnet add package Hyperbee.Expressions.Compiler
using Hyperbee.Expressions.Compiler;
var fn = HyperbeeCompiler.Compile( lambda );
Compilation speed: 9-34x faster than the System compiler. See Compiler for details.
Lab
Hyperbee.Expressions.Lab provides experimental expression types for HTTP fetch, JSON parsing, and collection map/reduce operations.
dotnet add package Hyperbee.Expressions.Lab
See Lab for details.
Credits
Special thanks to:
- Sergey Tepliakov – Dissecting the async methods in C#
- Fast Expression Compiler for improved performance
- Just The Docs for the documentation theme
Contributing
We welcome contributions! Please see our Contributing Guide for more details.