Skip to content

hell-racer/ExpressionToString

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

318 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Expression To String and Expression Tree Visualizer

This project provides the following:

Feedback

  • Star the project
  • File an issue

String representations of expression trees

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"));
// prints:
/*
    // using static System.Linq.Expressions.Expression

    Lambda(
        Constant(true)
    )
*/

Console.WriteLine(expr.ToString("Object notation"));
// 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("Textual tree"));
// prints:
/*
    Lambda (Func<bool>)
        Body - Constant (bool) = True
*/

Features:

  • Multiple formatters (with more planned):

    • 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
  • 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
  • Type names are rendered using language keywords, instead of just the type name; e.g. List<string> or List(Of Date) instead of List`1

  • Special handling of calls to String.Concat and String.Format

    var name = "World";
    Expression<Func<string>> expr = () => string.Format("Hello, {0}!", name);
    Console.WriteLine(expr.ToString("C#"));
    // prints: () => $"Hello, {name}!"
  • Supports the full range of types in System.Linq.Expressions, including .NET 4 expression types, and DynamicExpression

Visual Studio debugger visualizer for expression trees

The UI consists of:

  1. a graphical treeview of the expression tree structure,
  2. source code representation of the tree, and
  3. end nodes -- nodes in the tree which are not composed of other expressions: parameters, closure variables, constants and default values

Screenshot

You can switch formatters without reloading the visualizer:

Language switch

Selection syncing:

  • when selecting from the tree:

    Selection sync from tree

  • from source code:

    Selection sync from source code

  • and from end nodes:

    Selection sync from end nodes

Credits

About

String representation of expression trees and expression tree parts + debugging visualizer

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • C# 85.7%
  • Visual Basic .NET 14.3%