Friendly, Readable Expression Trees

tl;dr: ReadableExpressions is an extension method which makes a readable string from Expression trees that's on NuGet and GitHub. There's a set of Debug Visualizers which use it in the Visual Studio Gallery.

We all like playing around with working with Expression Trees, right? Creating type-safe functions at runtime when you don't know the types at compile time gives you great performance and is just plain neat. I'm using them in my pet object-object mapper, and need to look at the mapping functions it creates. Unfortunately, the default debug view for an Expression Tree looks something like this:

DebugView

…now maybe you're some coding savant who eats IL for breakfast, but I find that pretty unreadable.

So! To get a nicer look at my Expression Trees, I've written ReadableExpressions, a PCL with a single extension method which translates an Expression Tree into something friendlier, like:

VisualizerView

…yes, that's the same Expression Tree as the first screenshot Smile

Because I needed one, I also added an Expression for comments:

var comment = ReadableExpression.Comment("Anyone listening?");
Expression<Action> beep = () => Console.Beep();

var commentedBeep = Expression.Block(comment, beep.Body);

var translated = commentedBeep.ToReadableString();

const string EXPECTED = @"
// Anyone listening?
Console.Beep();";

Assert.AreEqual(EXPECTED.TrimStart(), translated);

Uses and How to Download

The ReadableExpressions NuGet package (Install-Package AgileObjects.ReadableExpressions) provides the extension method:

Expression<Func<string, string, int>> convertStringsToInt = (str1, str2) => int.Parse(str1) + int.Parse(str2);

var translated = convertStringsToInt.ToReadableString();

Assert.AreEqual("(str1, str2) => int.Parse(str1) + int.Parse(str2)", translated);

...and I've used it to make Expression Debug Visualizers for Visual Studio 10 to 15. They're in the root of the GitHub repo, or via an installer in the Visual Studio Gallery.

Print | posted @ Monday, February 29, 2016 7:30 PM

Comments on this entry:

Gravatar # re: Friendly, Readable Expression Trees
by Max at 3/1/2016 1:31 PM

This is pretty cool, any plans to upload to VS Gallery?
Gravatar # re: Friendly, Readable Expression Trees
by Steve at 3/1/2016 6:55 PM

Cheers! I've not written an extension before so I hadn't considered that, but yeah - I'll look into it :)
Gravatar # re: Friendly, Readable Expression Trees
by Steve Wilkes at 4/5/2016 8:56 PM

Aaaaaaaaand... I did. Blog updated, and more info at http://wblo.gs/jGs
Gravatar # re: Friendly, Readable Expression Trees
by Mr Lovely! at 5/17/2016 6:49 PM

Dude, that is a seriously cool extension method. Annoyed I don't currently have a use for it! Hope all is good.
Gravatar # re: Friendly, Readable Expression Trees
by Steve Wilkes at 5/17/2016 9:10 PM

Cheers, Si! My main use for it is the debugger visualizer - I use it every day :) All good here, hope all is well with you :)
Gravatar # re: Friendly, Readable Expression Trees
by James at 8/30/2016 8:24 PM

Any chance you could add an explicit license file to the source code on GitHub? The only license I can find is on the NuGet package.

By default, open source code is not usable for most if it doesn't have a license, and even looking at it is a bad idea.
Gravatar # re: Friendly, Readable Expression Trees
by Steve at 8/30/2016 9:38 PM

Absolutely! I've added an MIT licence file to the root of the repository.
Post A Comment
Title:
Name:
Email:
Comment:
Verification: