Visualizing (.NET 3.5) Expressions

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 on Visualizing (.NET 3.5) Expressions

  1. 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.

    Like

  2. 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 🙂

    Like

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s