background preloader

Expression Trees

Facebook Twitter

Automating null checks with Linq expressions « Thomas Levesque's .NET blog. The problem Have you ever written code like the following ?

Automating null checks with Linq expressions « Thomas Levesque's .NET blog

I bet you have !

Serialization Expression Trees

Expression Tree Basics - Charlie Calvert's Community Blog. Newcomers to LINQ often find expression trees difficult to grasp.

Expression Tree Basics - Charlie Calvert's Community Blog

In this post I hope to show that the subject is not quite as difficult as it might appear at first. Any reader who has an intermediate level understanding of LINQ should find the material in this post easy to grasp. Creating Property Set Expression Trees In A Developer Friendly Way. In a previous post, I showed how to create expression trees to set properties on an object.

Creating Property Set Expression Trees In A Developer Friendly Way

The way I did it was not very developer friendly. It involved explicitly creating the necessary expressions because the compiler won’t generate expression trees with property or field set expressions. Recently someone contacted me the help to develop some kind of command pattern framework that used developer friendly lambdas to generate property set expression trees.

Simply putting, given this entity class: public class Person { public string Name { get; set; } } The person in question wanted to write code like this: var et = Set((Person p) => p.Name = "me"); Where et is the expression tree that represents the property assignment. So, if we can’t do this, let’s try the next best thing that is splitting retrieving the property information from retrieving the value to assign to the property: var et = Set((Person p) => p.Name, () => "me"); C# - Altering expression. Lambda - How do I set a field value in an C# Expression tree. How can I get objects and property values from expression trees? - C# Frequently Asked Questions. This is a follow-up to the Getting Information About Objects, Types, and Members with Expression Trees post, so I would recommend that you read that one first.

How can I get objects and property values from expression trees? - C# Frequently Asked Questions

Among other code examples in that blog post, I demonstrated how you can get a property name as a string by using expression trees. Here is the method. public static string GetName<T>(Expression<Func<T>> e) { var member = (MemberExpression)e.Body; return member.Member.Name; } And here is how you can use it. When you go deep into expression trees, you may need to get the actual value of the property or the reference to the containing object out of the expression itself. Let’s say I want to create a method that prints not only the name of a property, but also the value of the property.

Now let’s look closely at this magic line: T value = e.Compile()(); In general, T is the type of the value that the expression produces. My Brain Hurts: Expression Trees, dynamic, and Multithreading in C# 3.0/4.0. I just had a brain fart the the other day: the dynamic keyword in C# 4.0 is more than just a way to plug interact dynamic languages.

My Brain Hurts: Expression Trees, dynamic, and Multithreading in C# 3.0/4.0

It can be used as a mechanism to evaluate any expression tree at runtime. One such example is that any arbitrary data source can be generically fashioned into dynamic objects that developers can interact with at run time. Take for example the following XML document: Given a proper dynamic binding implementation for XML, the following pseudo code could be used to interact with this document: This approach can be used to access or modify any potential data source, such as a databases, web services, RPC, etc. This train of thought continued until it derailed into another area I find interesting: multithreading (and parallel computing). LongOperation is an operation that takes 1 second and returns the value it was passed. Those operations are begging to be run asynchronously by way of threads. Consider the following code:

Generating Dynamic Methods with Expression Trees in Visual Studio 2010 - C# Frequently Asked Questions. C# 4 expressions: blocks [Part I] - Mitsu's blog. Since .Net 3.5 and Linq, the C# compiler is capable of generating expression trees instead of standard executable IL.

C# 4 expressions: blocks [Part I] - Mitsu's blog

Even if Linq opens the door of meta-programming (using the code to define something else, like a Sql query), we still have a lot of limitations. A C# expression is limited to a single instruction returning a value, given some parameters. Method calls, properties access, constructors, and operators are allowed but no block, no loops, etc… Expression Trees (C# and Visual Basic) Expression trees represent code in a tree-like data structure, where each node is an expression, for example, a method call or a binary operation such as x < y.

Expression Trees (C# and Visual Basic)

You can compile and run code represented by expression trees. This enables dynamic modification of executable code, the execution of LINQ queries in various databases, and the creation of dynamic queries. .Net Expression Evaluator using DynamicMethod. Download source - 36 Kb Introduction.

.Net Expression Evaluator using DynamicMethod

Composing Statements with Expression Trees in .NET 3.0. I was preparing some examples for my upcoming ALT.NET Workshop and thought that this might be of some interest to a few people.

Composing Statements with Expression Trees in .NET 3.0

Expression Trees’ primary raison d’être was to facilitate LINQ-y type stuff in the IQueryable implementations to support delayed execution and some other fun stuff. Expression Trees, however, go much father than that. They are essentially a window in to the middle of the compilation process in .NET. Imagine if you could hit PAUSE on the C# or VB compiler in mid-compile and take a snapshot of its internal state/model of the code before it does the final step of writing out the MSIL. What if you could not only capture it, but you could manipulate and add to it before finishing the compilation and executing it? We use Expression Trees a lot at Dovetail, in StructureMap, Fluent NHibernate, and various other projects primarily as a trick for fast reflection (static reflection). How to dynamically create an Expression<Func<T, object>>