Hyperbee.Expressions
1.0.0-develop.241104145928
See the version list below for details.
dotnet add package Hyperbee.Expressions --version 1.0.0-develop.241104145928
NuGet\Install-Package Hyperbee.Expressions -Version 1.0.0-develop.241104145928
<PackageReference Include="Hyperbee.Expressions" Version="1.0.0-develop.241104145928" />
paket add Hyperbee.Expressions --version 1.0.0-develop.241104145928
#r "nuget: Hyperbee.Expressions, 1.0.0-develop.241104145928"
// Install Hyperbee.Expressions as a Cake Addin #addin nuget:?package=Hyperbee.Expressions&version=1.0.0-develop.241104145928&prerelease // Install Hyperbee.Expressions as a Cake Tool #tool nuget:?package=Hyperbee.Expressions&version=1.0.0-develop.241104145928&prerelease
Welcome to Hyperbee Expressions
Hyperbee.Expressions
is a library for creating c# expression trees that extend the capabilities of standard expression
trees to handle asynchronous workflows and other language constructs.
Features
Async Expressions
AwaitExpression
: An expression that represents an await operation.AsyncBlockExpression
: An expression that represents an asynchronous code block.
Using Expression
UsingExpression
: An expression that automatically disposes IDisposable resources.
Looping Expressions
WhileExpression
: An expression that represents a while loop.ForExpression
: An expression that represents a for loop.ForEachExpression
: An expression that represents a foreach loop.
Supports Fast Expression Compiler (FEC) for improved performance.
Examples
Asynchronous Expressions
The following example demonstrates how to create an asynchronous expression tree.
When the expression tree is compiled, the BlockAsyncExpression
will auto-generate a state machine that executes
AwaitExpressions
in the block asynchronously.
the `BlockAsync` expression will generate a state machine that executes the expressions in the block asynchronously.
```csharp
public class AsyncExample
{
public async Task ExampleAsync()
{
// Variables to store the results
var result1 = Expression.Variable( typeof(int), "result1" );
var result2 = Expression.Variable( typeof(int), "result2" );
// Define two async method calls
var instance = Expression.Constant( this );
var awaitExpr1 = ExpressionExtensions.Await(
Expression.Call( instance, nameof(FirstAsyncMethod), Type.EmptyTypes )
);
var awaitExpr2 = ExpressionExtensions.Await(
Expression.Call( instance, nameof(SecondAsyncMethod), Type.EmptyTypes, result1 )
);
// Assign the results of the await
var assignResult1 = Expression.Assign( result1, awaitExpr1 );
var assignResult2 = Expression.Assign( result2, awaitExpr2 );
// Create an async block that calls both methods and assigns their results
var asyncBlock = AsyncExpression.BlockAsync(
[result1, result2],
assignResult1,
assignResult2
);
// Compile and execute the async block
var lambda = Expression.Lambda<Func<Task<int>>>( asyncBlock );
var compiledLambda = lambda.Compile();
var resultValue2 = await compiledLambda();
Console.WriteLine( $"Second async method result: {resultValue2}" );
}
public static async Task<int> FirstAsyncMethod()
{
await Task.Delay( 1000 ); // Simulate async work
return 42; // Example result
}
public static async Task<int> SecondAsyncMethod( int value )
{
await Task.Delay( 1000 ); // Simulate async work
return value * 2; // Example result
}
}
Using Expression
The following example demonstrates how to create a Using expression.
public class UsingExample
{
private class DisposableResource : IDisposable
{
public bool IsDisposed { get; private set; }
public void Dispose() => IsDisposed = true;
}
public void UsingExpression_ShouldDisposeResource_AfterUse()
{
var resource = new TestDisposableResource();
var disposableExpression = Expression.Constant( resource, typeof( TestDisposableResource ) );
var bodyExpression = Expression.Empty(); // Actual body isn't important
var usingExpression = ExpressionExtensions.Using(
disposableExpression,
bodyExpression
);
var compiledLambda = Expression.Lambda<Action>( reducedExpression ).Compile();
compiledLambda();
Console.WriteLine( $"Resource was disposed {resource.IsDisposed}." );
}
}
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.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. |
-
net8.0
- Microsoft.CodeAnalysis.CSharp (>= 4.11.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0-develop.241127193648 | 31 | 11/27/2024 |
1.0.0-develop.241126203740 | 39 | 11/26/2024 |
1.0.0-develop.241126181942 | 40 | 11/26/2024 |
1.0.0-develop.241125214236 | 40 | 11/25/2024 |
1.0.0-develop.241125205807 | 32 | 11/25/2024 |
1.0.0-develop.241121204357 | 36 | 11/21/2024 |
1.0.0-develop.241120202757 | 35 | 11/20/2024 |
1.0.0-develop.241119145709 | 32 | 11/19/2024 |
1.0.0-develop.241118213250 | 35 | 11/18/2024 |
1.0.0-develop.241104201905 | 39 | 11/4/2024 |
1.0.0-develop.241104145928 | 34 | 11/4/2024 |
1.0.0-develop.241031173234 | 40 | 10/31/2024 |
1.0.0-develop.241028171002 | 40 | 10/28/2024 |
1.0.0-develop.241025145716 | 40 | 10/25/2024 |
1.0.0-develop.241024140338 | 39 | 10/24/2024 |
1.0.0-develop.241016172242 | 51 | 10/16/2024 |