您好,以下内容是关于C# <T>(Expression<Func<T>>,求解释~

private string balance;
public string Balance
{
get { return balance; }
set
{
if (balance != value)
{
balance = value;
RaisePropertyChangedEvent(() => Balance); //这一句
}
}
}

//以下的代码
public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };

private void RaisePropertyChangedEvent<T>(Expression<Func<T>> propertyExpression)
{
PropertyChanged(this, new PropertyChangedEventArgs(GetPropertyName(propertyExpression)));
}

private string GetPropertyName<T>(Expression<Func<T>> propertyExpression)
{
var expression = propertyExpression.Body as System.Linq.Expressions.MemberExpression;
return expression.Member.Name;
}

最上面的是属性,下面这一大段是什么?起什么作用?

ibeautiful
浏览 285回答 2
2回答

缥缈止盈

1.GetPropertyName 这方法是通过Lambda表达式获取属性名。2.RaisePropertyChangedEvent 这个是触发PropertyChangedEventHandler事件并将对象的属性名做参数传入。

沧海一幻觉

抛开问题内容,对于 Expression<Func<TEntity, object>>应传一个参数为TEntity 返回值为object的表达式,直接返回需要的对象即可。对于问题内容中的方法,可以这样调用&nbsp;//假如我有个用户实体类&nbsp;public&nbsp;class&nbsp;UserInfoEntity&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;string&nbsp;Id&nbsp;{&nbsp;get;&nbsp;set;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;string&nbsp;UserName{&nbsp;get;&nbsp;set;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//调用&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;static&nbsp;void&nbsp;Main(string[]&nbsp;args)&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;List<UserInfoEntity>&nbsp;lst_u&nbsp;=&nbsp;Search((u)&nbsp;=>&nbsp;u.UserName);&nbsp;&nbsp;&nbsp;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP