开发者

NotificationObject & UnaryMember

I've seen numerous implementations of the INotifyPropertyChanged (Automatically INotifyPropertyChanged) that have support for th开发者_如何学Ce UnaryExpression in addition to the MemberExpression. Examples of the UnaryExpression include uses of unary operators within the expression such as,

RiasePropertyChanged(() => !MyPropertyName);

Is there reason enough to warrant its inclusion in the property name extraction? I had supposed the support for the UnaryExpression is because you could extract the MemberExpression from this.

Does the community have any pro/cons related this this implementation as it differs from the Prism NotificationObject?


I've only seen this in situations where someone wanted to support a wide variety of ways to refer to the member. Limiting it to UnaryExpression limits you slightly.

It doesn't really matter... adding support for both will be primarily a performance concern, but it will likely not make much difference in the end. I'd say support both just so you don't risk having the usage be more of a leaky abstraction than it already is.

Here's what we use:

public void OnPropertyChanged<TProperty>(Expression<Func<TProperty>> property)
{
    var lambda = (LambdaExpression)property;
    MemberExpression memberExpression;

    if ( lambda.Body is UnaryExpression )
    {
      var unaryExpression = (UnaryExpression)lambda.Body;
      memberExpression = (MemberExpression)unaryExpression.Operand;
    }
    else
    {
      memberExpression = (MemberExpression)lambda.Body;
    }
    OnPropertyChanged(memberExpression.Member.Name);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜