INotifyPropertyChanged的属性名称 - 硬code VS写照吗? [英] INotifyPropertyChanged property name - hardcode vs reflection?

查看:177
本文介绍了INotifyPropertyChanged的属性名称 - 硬code VS写照吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是使用INotifyPropertyChanged的时候指定属性名的最好方法是什么?

What is the best way to specify a property name when using INotifyPropertyChanged?

大多数例子很难code中的属性名称作为PropertyChanged事件的说法。我想使用MethodBase.GetCurrentMethod.Name.Substring(4),但我对反射的开销有点不安。

Most examples hardcode the property name as an argument on the PropertyChanged Event. I was thinking about using MethodBase.GetCurrentMethod.Name.Substring(4) but am a little uneasy about the reflection overhead.

推荐答案

不要忘记一件事:的PropertyChanged 事件主要消耗部件将使用反射,以获取指定属性的值。

Don't forget one thing : PropertyChanged event is mainly consumed by components that will use reflection to get the value of the named property.

最明显的例子就是数据绑定。

The most obvious example is databinding.

当你火的PropertyChanged 事件,传递属性的名称作为参数,你应该知道的本次活动的用户很可能使用反射的调用,例如,的getProperty (至少如果它使用的PropertyInfo的高速缓存第一次) ,那么的GetValue 。这最后一次通话是属性getter方法​​,它的价格超过了的getProperty 只查询元数据的动态调用(MethodInfo.Invoke)。 (请注意,数据绑定依赖于整个 TypeDescriptor 的事情 - 但默认实现使用反射。 )

When you fire PropertyChanged event, passing the name of the property as a parameter, you should know that the subscriber of this event is likely to use reflection by calling, for instance, GetProperty (at least the first time if it uses a cache of PropertyInfo), then GetValue. This last call is a dynamic invocation (MethodInfo.Invoke) of the property getter method, which costs more than the GetProperty which only queries meta data. (Note that data binding relies on the whole TypeDescriptor thing -- but the default implementation uses reflection.)

所以,当然使用硬code属性名称时射击的PropertyChanged比使用反射动态获取属性的名称更有效率,但恕我直言,以平衡你的想法是很重要的。在一些情况下,性能开销不是关键的,并且可以受益于某种上强类型事件击发机构

So, of course using hard code property names when firing PropertyChanged is more efficient than using reflection for dynamically getting the name of the property, but IMHO, it is important to balance your thoughts. In some cases, the performance overhead is not that critical, and you could benefit from some kind on strongly typed event firing mechanism.

下面是我在C#3.0中,当表演不会是一个问题,有时使用的:

Here is what I use sometimes in C# 3.0, when performances would not be a concern :

public class Person : INotifyPropertyChanged
{
    private string name;

    public string Name
    {
        get { return this.name; }
        set 
        { 
            this.name = value;
            FirePropertyChanged(p => p.Name);
        }
    }

    private void FirePropertyChanged<TValue>(Expression<Func<Person, TValue>> propertySelector)
    {
        if (PropertyChanged == null)
            return;

        var memberExpression = propertySelector.Body as MemberExpression;
        if (memberExpression == null)
            return;

        PropertyChanged(this, new PropertyChangedEventArgs(memberExpression.Member.Name));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

注意这里使用了EX pression树来获得属性的名称,以及使用的拉姆达EX pression为防爆pression

FirePropertyChanged(p => p.Name);

这篇关于INotifyPropertyChanged的属性名称 - 硬code VS写照吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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