Monday, 19 October 2009

Expression<TDelegate> Class

Documentation is here: http://msdn.microsoft.com/en-us/library/bb335710.aspx
The documentation states:
Represents a strongly typed lambda expression as a data structure in the form of an expression tree. This class cannot be inherited…
“…When a lambda expression is assigned to a variable, field, or parameter whose type is Expression<TDelegate>, the compiler emits instructions to build an expression tree…
…The expression tree is an in-memory data representation of the lambda expression. The expression tree makes the structure of the lambda expression transparent and explicit. You can interact with the data in the expression tree just as you can with any other data structure.
The ability to treat expressions as data structures enables APIs to receive user code in a format that can be inspected, transformed, and processed in a custom manner. For example, the LINQ to SQL data access implementation uses this facility to translate expression trees to Transact-SQL statements that can be evaluated by the database.”

Properties

Expression<TDelegate> has 4 properties:
  • Body – gets the body of the lambda expression.
  • NodeType – gets the node type of the expression (an enumeration of 45 different values representing all the different types of expression such as constants, greater than (>), less than (<) etc).
  • Parameters – gets the parameters of the lambda expression.
  • Type - gets the static type of the expression that this Expression represents (i.e. one of the Func types such as Func<T1, T2, TResult>).

Methods

There is really only one interesting method on Expression<TDelegate>:
  • Compile - compiles the lambda expression described by the expression tree into executable code.

Namespace

Expression<TDelegate> is part of the System.Linq.Expressions namespace which contains a number of interesting classes:
Class Description
BinaryExpression Represents an expression that has a binary operator.
ConditionalExpression Represents an expression that has a conditional operator.
ConstantExpression Represents an expression that has a constant value.
ElementInit Represents an initializer for a single element of an IEnumerable collection.
Expression Provides the base class from which the classes that represent expression tree nodes are derived. It also contains static (Shared in Visual Basic) factory methods to create the various node types. This is an abstract class.
Expression<TDelegate> Represents a strongly typed lambda expression as a data structure in the form of an expression tree. This class cannot be inherited.
InvocationExpression Represents an expression that applies a delegate or lambda expression to a list of argument expressions.
LambdaExpression Describes a lambda expression.
ListInitExpression Represents a constructor call that has a collection initializer.
MemberAssignment Represents initializing a field or property of a newly created object.
MemberBinding Provides the base class from which the classes that represent bindings that are used to initialize members of a newly created object derive.
MemberExpression Represents accessing a field or property.
MemberInitExpression Represents calling a constructor and initializing one or more members of the new object.
MemberListBinding Represents initializing the elements of a collection member of a newly created object.
MemberMemberBinding Represents initializing members of a member of a newly created object.
MethodCallExpression Represents calling a method.
NewArrayExpression Represents creating a new array and possibly initializing the elements of the new array.
NewExpression Represents a constructor call.
ParameterExpression Represents a named parameter expression.
TypeBinaryExpression Represents an operation between an expression and a type.
UnaryExpression Represents an expression that has a unary operator.
See http://msdn.microsoft.com/en-us/library/system.linq.expressions.aspx.
More to follow on some of these separate types.