13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Expression trees239code to run on your CPU. That’s quite deeply hidden, though, and while libraries existto manipulate IL programmatically, they’re not widely used.Expression trees <strong>in</strong> .NET 3.5 provide an abstract way of represent<strong>in</strong>g some code as atree of objects. It’s like CodeDOM but operat<strong>in</strong>g at a slightly higher level, and only forexpressions. The primary use of expression trees is <strong>in</strong> LINQ, and later <strong>in</strong> this sectionwe’ll see how crucial expression trees are to the whole LINQ story.<strong>C#</strong> 3 provides built-<strong>in</strong> support for convert<strong>in</strong>g lambda expressions to expressiontrees, but before we cover that let’s explore how they fit <strong>in</strong>to the .NET Frameworkwithout us<strong>in</strong>g any compiler tricks.9.3.1 Build<strong>in</strong>g expression trees programmaticallyExpression trees aren’t as mystical as they sound, although some of the uses they’reput to look like magic. As the name suggests, they’re trees of objects, where each node<strong>in</strong> the tree is an expression <strong>in</strong> itself. Different types of expressions represent the differentoperations that can be performed <strong>in</strong> code: b<strong>in</strong>ary operations, such as addition;unary operations, such as tak<strong>in</strong>g the length of an array; method calls; constructorcalls; and so forth.The System.L<strong>in</strong>q.Expressions namespace conta<strong>in</strong>s the various classes that representexpressions. All of them derive from the Expression class, which is abstract andmostly consists of static factory methods to create <strong>in</strong>stances of other expressionclasses. It exposes two properties, however:■■The Type property represents the .NET type of the evaluated expression—youcan th<strong>in</strong>k of it like a return type. The type of an expression that fetches theLength property of a str<strong>in</strong>g would be <strong>in</strong>t, for example.The NodeType property returns the k<strong>in</strong>d of expression represented, as a memberof the ExpressionType enumeration, with values such as LessThan, Multiply,and Invoke. To use the same example, <strong>in</strong> myStr<strong>in</strong>g.Length the property accesspart would have a node type of MemberAccess.There are many classes derived from Expression, and some of them can have manydifferent node types: B<strong>in</strong>aryExpression, for <strong>in</strong>stance, represents any operation withtwo operands: arithmetic, logic, comparisons, array <strong>in</strong>dex<strong>in</strong>g, and the like. This iswhere the NodeType property is important, as it dist<strong>in</strong>guishes between different k<strong>in</strong>dsof expressions that are represented by the same class.I don’t <strong>in</strong>tend to cover every expression class or node type—there are far toomany, and MSDN does a perfectly good job of expla<strong>in</strong><strong>in</strong>g them. Instead, we’ll try to geta general feel for what you can do with expression trees.Let’s start off by creat<strong>in</strong>g one of the simplest possible expression trees, add<strong>in</strong>g twoconstant <strong>in</strong>tegers together. List<strong>in</strong>g 9.6 creates an expression tree to represent 2+3.List<strong>in</strong>g 9.6 A very simple expression tree, add<strong>in</strong>g 2 and 3Expression firstArg = Expression.Constant(2);Expression secondArg = Expression.Constant(3);Expression add = Expression.Add(firstArg, secondArg);Console.WriteL<strong>in</strong>e(add);Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!