How RaisePropertyChanged<T> finds out the property name?

。_饼干妹妹 提交于 2020-01-12 13:56:31

问题


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 ?


回答1:


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


来源:https://stackoverflow.com/questions/10243327/how-raisepropertychangedt-finds-out-the-property-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!