Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How RaisePropertyChanged<T> finds out the property name?

There is one overload of this method in NotificationObject :-

protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression);

We write in the following way in the setter of property:

RaisePropertyChanged(() => PropertyVariable);

How does it works ? How it finds the property name out of this Lambda expression ?

like image 739
teenup Avatar asked Apr 20 '12 09:04

teenup


1 Answers

An Expression<TDelegate> represents the abstract syntax tree of the lambda expression. So you just have to analyze this syntax tree to find out the property name:

protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
{
    var memberExpr = propertyExpression.Body as MemberExpression;
    if (memberExpr == null)
        throw new ArgumentException("propertyExpression should represent access to a member");
    string memberName = memberExpr.Member.Name;
    RaisePropertyChanged(memberName);
}
like image 59
Thomas Levesque Avatar answered Nov 10 '22 06:11

Thomas Levesque