BT

New Early adopter or innovator? InfoQ has been working on some new features for you. Learn more

Edit Expression Trees with MetaLinq

| by Jonathan Allen Follow 6 Followers on Jun 14, 2007. Estimated reading time: 1 minute |

Expression trees are a powerful feature in any language. LINQ uses them under the covers to execute object queries, while DLINQ to SQL transforms them into SQL queries. Other people are using them to generate code on the fly. But like any other feature, there are design tradeoffs.

In order to better support things like query comprehension and function composition, expression trees are immutable. Libraries are guaranteed that once they have a reference to an expression tree, that expression tree will never change. Whole classes of bugs are eliminated by doing this, though it does make modified versions of an expression tree somewhat difficult.

Developers wishing to edit one have to manually build new expressions by copying existing ones. Aaron Erickson's MetaLinq allows developers to side-step this by providing a way to convert expression trees into mutable representations and back again.

MetaLinq does this with what Aaron calls an Editable Shadow. Developers create this shadow by passing an expression to the CreateEditableExpression method. When done making changes, they simply need to call ToExpression to get a usable expression tree again.

MetaLinq isn't the only way to edit expression trees. Jomo Fisher demonstrates a visitor pattern than will walk an expression tree, creating a new tree while swapping out specific nodes. While this technique is more complex conceptually, it does allow for some interesting search and replace techniques.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT