« On my way to Basta!Installing AUCTeX in EmacsW32 »

Visualizing (.NET 3.5) Expressions

06/09/07

Permalink 10:45:50 am
Categories: General, Programming, .NET

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&gt;). 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

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
09/06/07 @ 12:33
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.
09/06/07 @ 12:45
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 :)
09/10/07 @ 13:22

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
Please complete the song title below. Hint: enter 'h', 'e', 'a', 'v', 'e', 'n'
antispam test

Enter your email address:

Search

Oliver
MVP logo
March 2010
Sun Mon Tue Wed Thu Fri Sat
 << <   > >>
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31