TagBites.Expressions 1.0.7

dotnet add package TagBites.Expressions --version 1.0.7                
NuGet\Install-Package TagBites.Expressions -Version 1.0.7                
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="TagBites.Expressions" Version="1.0.7" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TagBites.Expressions --version 1.0.7                
#r "nuget: TagBites.Expressions, 1.0.7"                
#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.
// Install TagBites.Expressions as a Cake Addin
#addin nuget:?package=TagBites.Expressions&version=1.0.7

// Install TagBites.Expressions as a Cake Tool
#tool nuget:?package=TagBites.Expressions&version=1.0.7                

TagBites.Expressions

Nuget License

Converts C# text expressions into LINQ expressions using Roslyn, supporting complete language syntax.

Examples

public void BasicTest()
{
    var result = ExpressionParser.Invoke("5 / 2.5");
    Assert.Equal(2d, result);

    var result2 = ExpressionParser.Invoke<int>("new [] { 1, 2, 3 }.Select(x => (x, x + 1).Item2).Sum()");
    Assert.Equal(9, result2);

    var result3 = ExpressionParser.Invoke<int>("(a + b) / 2", ("a", 2), ("b", 4));
    Assert.Equal(3, result3);
}

public void GlobalMembersTest()
{
    var expressionText = "a switch { 1 => b, 2 => b * 2, _ => b + a }";

    var options = new ExpressionParserOptions
    {
        Parameters =
        {
            (typeof(int), "a")
        },
        GlobalMembers =
        {
            {"b", (null, 2)}
        }
    };
    var func = ExpressionParser.Compile<Func<int, int>>(expressionText, options);

    Assert.Equal(2, func(1));
    Assert.Equal(4, func(2));
    Assert.Equal(5, func(3));
}

public void TypeTest()
{
    var options = new ExpressionParserOptions
    {
        Parameters =
        {
            (typeof(TestModel), "this")
        },
        UseFirstParameterAsThis = true
    };

    var m = new TestModel { X = 1, Y = 2 };

    Assert.Equal(3, ExpressionParser.Invoke("this.X + this.Y", options, m));
    Assert.Equal(3, ExpressionParser.Invoke("X + Y", options, m));
    Assert.Equal(3, ExpressionParser.Invoke("X + new TestModel { X = 1, Y = 2 }.Y", options, m));
}

class TestModel
{
    public int X { get; set; }
    public int Y { get; set; }
}

Benchmark

Benchmark for parsing the string "Math.Pow(x, y) + 5" into a LINQ expression.
Comparison of TagBites (v. 1.0.5) and DynamicExpresso (v. 2.17.2).

Method Mean Error StdDev Allocated
TagBites 8.355 us 0.1583 us 0.1555 us 8.3 KB
TagBites_SharedOptions 7.780 us 0.1424 us 0.1262 us 8.04 KB
DynamicExpresso 26.860 us 0.3166 us 0.2962 us 35.12 KB
DynamicExpresso_SharedInterpreter 17.087 us 0.0903 us 0.0800 us 16.64 KB
[MemoryDiagnoser]
public class ParseToExpression
{
    private const string Script = "Math.Pow(x, y) + 5";

    private readonly ExpressionParserOptions _options = new()
    {
        Parameters =
        {
            (typeof(double), "x"),
            (typeof(double), "y")
        }
    };
    private readonly Interpreter _interpreter = new();


    [Benchmark]
    public void TagBites()
    {
        var options = new ExpressionParserOptions
        {
            Parameters =
            {
                (typeof(double), "x"),
                (typeof(double), "y")
            }
        };

        ExpressionParser.Parse(Script, options);
    }

    [Benchmark]
    public void TagBitesSharedOptions()
    {
        ExpressionParser.Parse(Script, _options);
    }

    [Benchmark]
    public void DynamicExpresso()
    {
        var interpreter = new Interpreter();

        interpreter.Parse(Script,
            new Parameter("x", typeof(double)),
            new Parameter("y", typeof(double)));
    }

    [Benchmark]
    public void DynamicExpressoSharedInterpreter()
    {
        _interpreter.Parse(Script,
            new Parameter("x", typeof(double)),
            new Parameter("y", typeof(double)));
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.7 143 3/23/2025
1.0.6 205 3/5/2025
1.0.5 195 3/4/2025
1.0.2 854 10/24/2024
1.0.1 1,430 1/18/2024