ExpressionTreeToString 3.4.70

Install-Package ExpressionTreeToString -Version 3.4.70
dotnet add package ExpressionTreeToString --version 3.4.70
<PackageReference Include="ExpressionTreeToString" Version="3.4.70" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ExpressionTreeToString --version 3.4.70
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ExpressionTreeToString, 3.4.70"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install ExpressionTreeToString as a Cake Addin
#addin nuget:?package=ExpressionTreeToString&version=3.4.70

// Install ExpressionTreeToString as a Cake Tool
#tool nuget:?package=ExpressionTreeToString&version=3.4.70
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Expression Tree To String

AppVeyor build status Tests NuGet Status       Test objects: NuGet TestObjects Status

Targets .NET Standard 2.0 and above / Framework 4.5.2 and above

Provides a ToString extension method which returns a string representation of an expression tree (an object inheriting from System.Linq.Expressions.Expression).

Expression<Func<bool>> expr = () => true;

Console.WriteLine(expr.ToString("C#"));
// prints: () => true

Console.WriteLine(expr.ToString("Visual Basic"));
// prints: Function() True

Console.WriteLine(expr.ToString("Factory methods", "C#"));
// prints:
/*
    // using static System.Linq.Expressions.Expression

    Lambda(
        Constant(true)
    )
*/

Console.WriteLine(expr.ToString("Object notation", "C#"));
// prints:
/*
    new Expression<Func<bool>> {
        NodeType = ExpressionType.Lambda,
        Type = typeof(Func<bool>),
        Body = new ConstantExpression {
            Type = typeof(bool),
            Value = true
        },
        ReturnType = typeof(bool)
    }
*/

Console.WriteLine(expr.ToString("Object notation", "Visual Basic"));
// prints:
/*
    New Expression(Of Func(Of Boolean)) With {
        .NodeType = ExpressionType.Lambda,
        .Type = GetType(Func(Of Boolean)),
        .Body = New ConstantExpression With {
            .Type = GetType(Boolean),
            .Value = True
        },
        .ReturnType = GetType(Boolean)
    }
*/

Console.WriteLine(expr.ToString("Textual tree"));
// prints:
/*
    Lambda (Func<bool>)
        Body - Constant (bool) = True
*/

var b = true;
Expression<Func<int>> expr1 = () => b ? 1 : 0;
Console.WriteLine(expr1.ToString("ToString"));
// prints:
/*
    () => IIF(value(_tests.Program+<>c__DisplayClass0_0).b, 1, 0)
*/

Console.WriteLine(expr1.ToString("DebugView"));
// prints:
/*
    .Lambda #Lambda1<System.Func`1[System.Int32]>() {
        .If (
            .Constant<_tests.Program+<>c__DisplayClass0_0>(_tests.Program+<>c__DisplayClass0_0).b
        ) {
            1
        } .Else {
            0
        }
    }
*/

Expression<Func<Person, bool>> filter = p => p => p.LastName == "A" || p.FirstName == "B" || p.DOB == DateTime.MinValue || p.LastName == "C" || p.FirstName == "D";
Console.WriteLine(filter.ToString("Dynamic LINQ", "C#"));
// prints:
/*
    LastName in ("A", "C") || DOB == DateTime.MinValue || FirstName in ("B", "D")
*/

Features:

  • Multiple writers:

    • Pseudo-code in C# or VB.NET
    • Factory method calls which generate this expression
    • Object notation, using object initializer and collection initializer syntax to describe objects
    • Textual tree, focusing on the properties related to the structure of the tree
    • ToString and DebugView reimplementation
    • Dynamic LINQ equivalent to the expression
  • For C# and VB pseudo-code representations:

    • Extension methods are rendered as instance methods

      Expression<Func<int, int>> expr = x => Enumerable.Range(1, x).Select(y => x * y).Count();
      Console.WriteLine(expr.ToString("C#"));
      // prints: (int x) => Enumerable.Range(1, x).Select((int y) => x * y).Count()
      
    • Closed-over variables are rendered as simple identifiers (instead of member access on the hidden compiler-generated class)

      var i = 7;
      var j = 8;
      Expression<Func<int>> expr = () => i + j;
      Console.WriteLine(expr.ToString("C#"));
      // prints: () => i + j
      
    • Calls to String.Concat and String.Format are mapped to the + operator and string interpolation, respectively (where possible):

      var name = "World";
      Expression<Func<string>> expr = () => string.Format("Hello, {0}!", name);
      Console.WriteLine(expr.ToString("C#"));
      // prints: () => $"Hello, {name}!"
      
    • Unnecessary conversions are not rendered:

      Expression<Func<IEnumerable<char>>> expr = () => (IEnumerable<char>)"abcd";
      Console.WriteLine(expr.ToString("C#"));
      // prints: () => "abcd"
      
    • Comparisons against an enum or char are rendered properly, not as comparison to int-converted value:

      var dow = DayOfWeek.Sunday;
      Expression<Func<bool>> expr = () => DateTime.Today.DayOfWeek == dow;
      
      Console.WriteLine(expr.ToString("Textual tree", "C#"));
      // prints:
      /*
        Lambda (Func<bool>)
            · Body - Equal (bool) = false
                · Left - Convert (int) = 3
                    · Operand - MemberAccess (DayOfWeek) DayOfWeek = DayOfWeek.Wednesday
                        · Expression - MemberAccess (DateTime) DateTime.Today = 30/09/2020 12:00:00 am
                · Right - Convert (int) = 0
                    · Operand - MemberAccess (DayOfWeek) dow = DayOfWeek.Sunday
                        · Expression - Constant (<closure>) = #<closure>      
      */
      
      Console.WriteLine(expr.ToString("C#"));
      // prints: () => DateTime.Today.DayOfWeek == dow
      
  • Each representation (including the ToString and DebugView renderers) can return the start and length of the substring corresponding to any of the paths of the tree's nodes, which can be used to find the substring corresponding to a given node in the tree:

    var s = expr.ToString("C#", out var pathSpans);
    Console.WriteLine(s);
    // prints: (Person p) => p.DOB.DayOfWeek == DayOfWeek.Tuesday
    
    (int start, int length) = pathSpans["Body.Left.Operand"];
    Console.WriteLine(s.Substring(start, length));
    // prints: p.DOB.DayOfWeek
    
  • Type names are rendered using language syntax and keywords, instead of the Type.Name property; e.g. List<string> or List(Of Date) instead of List`1

  • Supports the full range of types in System.Linq.Expressions, including .NET 4 expression types, and DynamicExpression

  • Extensibility -- allows creating custom renderers, or inheriting from existing renderers, to handle your own Expression-derived types

For more information, see the wiki.

Feedback

  • Star the project
  • File an issue

NuGet packages (4)

Showing the top 4 NuGet packages that depend on ExpressionTreeToString:

Package Downloads
ExpressionTreeVisualizer.UI

UI pieces for displaying expression trees

PackSite.Library.Modeling.Specifications

Querying specifications

GCore.Logging.Expressions

Useful C# Stuff for .NET Standard 2.0

AzureFunctions.AcceptanceTestRunner

The Azure Function Acceptance Tests Helpers to run remote, in-memory tests

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on ExpressionTreeToString:

Repository Stars
zspitz/ExpressionTreeVisualizer
Debugging visualizer for expression trees
Version Downloads Last updated
3.4.70 351 12/23/2021
3.4.68 183 12/14/2021
3.4.67 56 12/14/2021
3.4.65 8,138 7/14/2021
3.4.64 120 7/14/2021
3.4.63 905 7/5/2021
3.4.62 185 6/29/2021
3.4.61 313 6/23/2021
3.4.60 131 6/23/2021
3.3.59 173 6/17/2021
3.3.58 228 6/7/2021
3.3.57 124 6/6/2021
3.3.56 200 5/31/2021
3.2.54 36,205 1/16/2021
3.2.53 6,480 12/6/2020
3.2.52 297 12/5/2020
3.2.51 300 12/5/2020
3.2.50 290 12/5/2020
3.2.49 174 12/5/2020
3.2.48 1,883 10/28/2020
3.1.47 222 10/8/2020
3.1.45 341 9/30/2020
3.1.44 302 9/30/2020
3.1.43 216 9/24/2020
3.1.42 244 9/23/2020
3.0.41 291 9/16/2020
2.2.40 233 9/15/2020
2.2.39 229 9/15/2020
2.2.38 205 9/14/2020
2.2.37 295 8/31/2020
2.2.36 213 8/26/2020
2.1.34 213 8/24/2020
2.1.33 187 8/24/2020
2.0.32 233 8/18/2020
2.0.29 657 8/5/2020
2.0.28 274 7/19/2020
2.0.27 1,922 7/19/2020
2.0.26 318 7/19/2020
2.0.25 228 7/16/2020
2.0.23 292 6/28/2020
2.0.22 337 6/27/2020
2.0.21 312 6/27/2020
2.0.20 1,836 3/29/2020
2.0.19 356 3/29/2020
2.0.15 497 12/18/2019
2.0.14 279 12/18/2019
2.0.13 282 12/18/2019
2.0.12 257 12/18/2019
2.0.11 283 12/18/2019
2.0.10 320 12/16/2019
2.0.4 279 12/15/2019
1.6.2 298 12/15/2019
1.5.67 1,330 8/15/2019
1.5.66 386 8/14/2019
1.5.65 406 8/13/2019
1.4.64 406 8/1/2019
1.4.63 406 7/4/2019
1.4.62 390 7/4/2019
1.4.57 387 6/26/2019
1.3.55 410 6/25/2019
1.3.54 409 6/25/2019
1.3.53 400 6/24/2019
1.3.52 404 6/23/2019
1.3.51 399 6/21/2019
1.3.50 408 6/21/2019
1.3.49 393 6/21/2019
1.3.41 398 6/20/2019
1.3.35 409 6/18/2019
1.3.34 432 5/30/2019
1.3.33 418 5/30/2019
1.2.32 418 5/30/2019
1.2.30 420 5/30/2019
1.2.29 402 5/29/2019
1.2.28 450 5/21/2019
1.2.27 417 5/14/2019
1.2.19 828 5/3/2019
1.1.10 455 3/24/2019
1.1.7 435 3/15/2019
1.1.6 417 3/14/2019
1.1.5 404 3/14/2019
1.0.1 503 2/17/2019
1.0.0 504 2/14/2019

Non-statement expression support is complete, escaped string literals, exceptions written into source