In C#, why Expression Trees and when do you need to use them?

c# c#-3.0 expression-trees lambda

Question

When I need the Expression Trees ?

And please provide us with a real world sample if available

Popular Answer

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));
    }
}



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why