Hyperbee.Expressions 1.0.0-develop.241104201905

This is a prerelease version of Hyperbee.Expressions.
There is a newer version of this package available.
See the version list below for details.
The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package Hyperbee.Expressions --version 1.0.0-develop.241104201905
                    
NuGet\Install-Package Hyperbee.Expressions -Version 1.0.0-develop.241104201905
                    
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="Hyperbee.Expressions" Version="1.0.0-develop.241104201905" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Hyperbee.Expressions" Version="1.0.0-develop.241104201905" />
                    
Directory.Packages.props
<PackageReference Include="Hyperbee.Expressions" />
                    
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 Hyperbee.Expressions --version 1.0.0-develop.241104201905
                    
#r "nuget: Hyperbee.Expressions, 1.0.0-develop.241104201905"
                    
#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 Hyperbee.Expressions@1.0.0-develop.241104201905
                    
#: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=Hyperbee.Expressions&version=1.0.0-develop.241104201905&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Hyperbee.Expressions&version=1.0.0-develop.241104201905&prerelease
                    
Install as a Cake Tool

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:

Contributing

We welcome contributions! Please see our Contributing Guide for more details.

Product 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.  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 was computed.  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 (3)

Showing the top 3 NuGet packages that depend on Hyperbee.Expressions:

Package Downloads
Hyperbee.XS

XS: A Lightweight, Extensible Scripting Language for Expression Trees.

Hyperbee.XS.Extensions

Expression Script [XS] language extensions.

Hyperbee.Expressions.Lab

Sample Extentions for .NET Expression Trees.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.0 696 5/9/2025
1.2.0-develop.250417163546 191 4/17/2025
1.1.5 469 4/2/2025
1.1.5-develop.250402163927 168 4/2/2025
1.1.5-develop.250402142908 142 4/2/2025
1.1.5-develop.250402133114 150 4/2/2025
1.1.5-develop.250402131116 159 4/2/2025
1.1.5-develop.250401194515 139 4/1/2025
1.1.4 239 3/31/2025
1.1.4-develop.250331172856 161 3/31/2025
1.1.4-develop.250328154856 148 3/28/2025
1.1.3 275 3/19/2025
1.1.3-develop.250319162912 145 3/19/2025
1.1.3-develop.250317193155 165 3/17/2025
1.1.3-develop.250317174611 157 3/17/2025
1.1.3-develop.250313195935 159 3/13/2025
1.1.3-develop.250313192012 136 3/13/2025
1.1.2 697 1/28/2025
1.1.2-develop.250108191742 203 1/8/2025
1.1.1 289 1/6/2025
1.1.0 246 12/23/2024
1.1.0-develop.241220155532 96 12/20/2024
1.0.1 147 12/10/2024
1.0.0 157 12/5/2024
1.0.0-develop.241205135900 110 12/5/2024