相关疑难解决方法(0)

实现INotifyPropertyChanged时,[CallerMemberName]与替代品相比是否较慢?

有好文章提出了不同的实施方式INotifyPropertyChanged.

考虑以下基本实现:

class BasicClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private void FirePropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }

    private int sampleIntField;

    public int SampleIntProperty
    {
        get { return sampleIntField; }
        set
        {
            if (value != sampleIntField)
            {
                sampleIntField = value;
                FirePropertyChanged("SampleIntProperty"); // ouch ! magic string here
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我想用这个替换它:

using System.Runtime.CompilerServices;

class BetterClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    // Check the attribute …
Run Code Online (Sandbox Code Playgroud)

c# inotifypropertychanged callermembername

90
推荐指数
1
解决办法
3万
查看次数