Please click this logo to help me get on their beta program:

Xobni outlook add-in for your inbox








6/9/2007

Visualizing (.NET 3.5) Expressions

Filed under: General, Programming, .NET — Oliver Sturm @ 10:45 am - 1 year, 2 months ago

It’s a problem, when you work with Expression types in any way, to understand the structure of complex expressions and possibly recreate them. After all, there are 46 entries in the ExpressionType enum, many of them corresponding to their own Expression-derived type, all of those with their own specific properties… in other words, it’s not entirely intuitive.

To help visualization of Expressions, I have created the ExpressionDumper, which outputs any expression (by default) in a nice hierarchical way on the console. It shouldn’t be too hard to use the class with other output targets, and I’m placing it under GNU LGPL license for others to use (that allows commercial use, in case you’re not quite on top of all the various open source licenses <g>). I’d appreciate being kept up to date with any further development you may do on it, but you don’t have to tell me if you don’t want to.

With this source code

Expression<Predicate<Product>> expression = p => p.UnitPrice > 10;
ExpressionDumper.Output(expression);

the ExpressionDumper is going to produce this output (of course this is a very simple example, chosen for brevity):

LambdaExpression (
  Parameters:
    ParameterExpression (p)
  Body:
    BinaryExpression:GreaterThan (
      Method: Boolean op_GreaterThan(System.Decimal, System.Decimal)
      Left:
        MemberExpression Product.UnitPrice (
          Expression:
            ParameterExpression (p)
        )
      Right:
        ConstantExpression (10)
    )
)

Without further ado, here’s the download: ExpressionDumper.zip (5159 bytes) 

Have fun!

3 Comments »

  1. You do know there’s an Expression Tree debugger visualizer ? :) ->
    http://footheory.com/blogs/bennie/archive/2007/08/18/new-features-in-c-3-0-part-6-expression-trees.aspx

    Comment by Frans Bouma — 6/9/2007 @ 12:33 pm - 1 year, 2 months ago

  2. Hey Frans -
    No, actually I didn’t - seeing as it’s not even in beta 2 by default, I don’t think I’ll be the only one :-)

    Funny though, before I started doing this (a few weeks ago), I thought I’d read or heard something about such a tool, but I wasn’t able to find it again. Tried to ask people, but nobody knew… well, that’s how it goes.

    In any case, having a stand-alone implementation still sounds like a good idea to me. It’s certainly easier to show in a presentation, with a big font in a console window, than clicking around in the debugger visualizer.

    Comment by Oliver Sturm — 6/9/2007 @ 12:45 pm - 1 year, 2 months ago

  3. Yes, I think you’re right. I’ll first start with the shipped visualizer to see if I can use that to check whether the expression trees are shaped the way I think they are shaped (with the lack of serious documentation) but indeed a dumper is also good to have. Perhaps I’ll use yours as well and if necessary I’ll update it (and post you the code)

    Thanks for sharing :)

    Comment by Frans Bouma — 10/9/2007 @ 1:22 pm - 1 year, 2 months ago

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>


Powered by WordPress
© Copyright 2005-2008 Oliver Sturm