views:

164

answers:

4

When I need the Expression Trees ?

And please provide us with a real world sample if available

Thanks in advance

+5  A: 

For example to implement a typesafe implementation of INotifyPropertyChanged instead of using strings:

public class Sample : TypeSafeNotifyPropertyChanged
{
    private string _text;

    public string Text
    {
        get { return _text; }
        set
        {
            if (_text == value)
                return;

            _text = value;
            OnPropertyChanged(() => Text);
        }
    }
}

public class TypeSafeNotifyPropertyChanged : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged<T>(Expression<Func<T>> propertyExpression)
    {
        PropertyChangedHelper.RaisePropertyChanged(this, propertyExpression, PropertyChanged);
    }
}

public static class PropertyChangedHelper
{
    public static void RaisePropertyChanged<T>(object sender, Expression<Func<T>> propertyExpression, PropertyChangedEventHandler propertyChangedHandler)
    {
        if (propertyChangedHandler == null)
            return;

        if (propertyExpression.Body.NodeType != ExpressionType.MemberAccess)
            return;

        MemberExpression memberExpr = (MemberExpression)propertyExpression.Body;
        string propertyName = memberExpr.Member.Name;
        RaisePropertyChanged(sender, propertyName, propertyChangedHandler);
    }

    private static void RaisePropertyChanged(object sender, string property, PropertyChangedEventHandler propertyChangedHandler)
    {
        if (propertyChangedHandler != null)
            propertyChangedHandler(sender, new PropertyChangedEventArgs(property));
    }
}
Scordo
This is a good example of something where Expression trees come in handy, but I’d argue that ① it is not really a primary example, it only happens to be convenient; and ② you posted too much code and too little explanation. ☺
Timwi
Yeah, what Timwi said, is the purpose of all that code just to save people from typos/property name changes? If so that's useful I guess, but I'm not sure that's what it's for..
Jimmy Hoffa
This is to be primed for refactorings like renames.
Scordo
In the example `() => Text` the property name `Text` can be erroneously replaced with any expression at all, and it will still compile just fine. If the programmer doesn't supply the identifier of a member, then it silently fails to raise the event at runtime. So in what sense is this "type safe"?
Daniel Earwicker
+3  A: 

You need an expression tree every time you want to tell some function what needs to be done instead of actually doing it.

The prime example is LINQ-to-SQL. You pass an expression tree so that it can translate this expression tree into SQL. It doesn’t execute the expression, it examines it and translates it to SQL.

Timwi
Your first sentence sounds more like a description of a delegate than an expression tree. Whether it "needs to be done" and what exactly is "done" or not is purely down to the consumer of the expression.
x0n
+2  A: 

Expression tree is description of some execution - actually it is DOM. When you execute expression tree it is compiled and after that executed. Example is LinqToSql. When you build query for LinqToSql you are doing it on the IQueryable. Resulting query is expression tree. When you execute the tree it is "compiled" into SQL instead of .NET and executed on database.

Edit: Here you have another nice example of Expression used for including related entities in EF query.

Ladislav Mrnka
+1  A: 

I recently refactored a complex object comparer and used expression trees. The object comparer had been implemented using reflection, and I modified it to instead build an expression tree using reflection, then compile the expression tree into a delegate. There is a bit of expense to build and compile the delegate, but after it is compiled it is almost 100x faster to call than the reflected solution.

Mark