Skip to content

Available renderers

Zev Spitz edited this page Aug 19, 2020 · 15 revisions
  • C# -- C#-style pseudocode

    Console.WriteLine(expr.ToString("C#"));
    /*
      (Person p) => p.DOB.DayOfWeek == DayOfWeek.Tuesday
    */
  • Visual Basic -- Visual Basic -style pseudocode

    Console.WriteLine(expr.ToString("Visual Basic"));
    /*
      Function(p As Person) p.DOB.DayOfWeek = DayOfWeek.Tuesday
    */
  • Factory methods -- factory method calls used to generate the expression:

    Console.WriteLine(expr.ToString("Factory methods"));
    /*
      // using static System.Linq.Expressions.Expression
    
      Lambda(
          Equal(
              Convert(
                  MakeMemberAccess(
                      MakeMemberAccess(p,
                          typeof(Person).GetProperty("DOB")
                      ),
                      typeof(DateTime).GetProperty("DayOfWeek")
                  ),
                  typeof(int)
              ),
              Constant(2)
          ),
          var p = Parameter(
              typeof(Person),
              "p"
          )
      )
    */

    Note that you need to reuse the same parameter object across the entire expression tree, so we use a non-syntactical inline variable declaration (var p = ...). Ideally this declaration should be before the first function call; this is tracked in https://github.com/zspitz/ExpressionTreeToString/issues/8.

  • Object notation -- describes objects and collections using initializer syntax:

    Console.WriteLine(expr.ToString("Object notation"));
    /*
      new Expression<Func<Person, bool>> {
          NodeType = ExpressionType.Lambda,
          Type = typeof(Func<Person, bool>),
          Parameters = new ReadOnlyCollection<ParameterExpression> {
              new ParameterExpression {
                  Type = typeof(Person),
                  IsByRef = false,
                  Name = "p"
              }
          },
          Body = new BinaryExpression {
              NodeType = ExpressionType.Equal,
              Type = typeof(bool),
              Left = new UnaryExpression {
                  NodeType = ExpressionType.Convert,
                  Type = typeof(int),
                  Operand = new MemberExpression {
                      Type = typeof(DayOfWeek),
                      Expression = new MemberExpression {
                          Type = typeof(DateTime),
                          Expression = new ParameterExpression {
                              Type = typeof(Person),
                              IsByRef = false,
                              Name = "p"
                          },
                          Member = typeof(Person).GetProperty("DOB")
                      },
                      Member = typeof(DateTime).GetProperty("DayOfWeek")
                  }
              },
              Right = new ConstantExpression {
                  Type = typeof(int),
                  Value = 2
              }
          },
          ReturnType = typeof(bool)
      }
    */
  • Textual tree -- describes the structure of the expression tree: node type, reflection type, name and value, as appropriate

    Console.WriteLine(expr.ToString("Textual tree"));
    /*
      Lambda (Func<Person, bool>)
          Parameters[0] - Parameter (Person) p
          Body - Equal (bool)
              Left - Convert (int)
                  Operand - MemberAccess (DayOfWeek) DayOfWeek
                      Expression - MemberAccess (DateTime) DOB
                          Expression - Parameter (Person) p
              Right - Constant (int) = 2
    */
Clone this wiki locally