T> RaisePropertyChanged&LT如何;发现属性名称? [英] How RaisePropertyChanged<T> finds out the property name?

查看:487
本文介绍了T> RaisePropertyChanged&LT如何;发现属性名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前这个方法的一个重载 NotificationObject : -

There is one overload of this method in NotificationObject :-

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

我们写在财产的setter方法​​如下:

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

RaisePropertyChanged(() => PropertyVariable);

它是如何工作的?它是如何找到的属性名出这LAMBDA EX pression?

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

推荐答案

这是防爆pression&LT; TDelegate&GT; 重presents的抽象语法树拉姆达EX pression。所以,你只需要分析这个语法树,找出属性名称:

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

这篇关于T&GT; RaisePropertyChanged&LT如何;发现属性名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆