C#使用INotifyPropertyChange压缩类中的冗余代码

时间:2015-10-21 17:39:04

标签: c# class inotifypropertychanged

过去几天我一直在阅读一些关于继承类和创建基类的文章,我经常在我编写的工具中这样做。但是我特意在研究减少通常在包含INotifyPropertyChange的类中编写的冗余代码的方法。通常我的类看起来像这样,继承NotifyBase的基类。但是我在这里看到的是各种脚本中的人将一些Get和Set代码移动到基类中。我想知道做这件事时要注意什么?这是一种不良做法还是良好做法?我提供的示例是否正确写入?

Get和Set的一个好处是在新设置中继承NotifyBase的类中更加简单。

当前设置示例

FileItem类

using System.IO;

namespace WpfApplication1
{
    public class FileItem : NotifyBase
    {
        private string _fullPath;
        public string FullPath
        {
            get { return this._fullPath; }
            set
            {
                this._fullPath = value;
                this.NotifyPropertyChanged("FullPath");
            }
        }
    }
}

基类' NotifyBase '

using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace WpfApplication1
{
    public class NotifyBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

可能的新设置

FileItem类

using System.IO;

namespace WpfApplication1
{
    public class FileItem : NotifyBase
    {
        public string FullPath
        {
            get { return Get<string>(); }
            set { Set(value); }
        }
    }
}

基类&#39; NotifyBase &#39;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace Varo.Helper
{
    public class NotifyBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private readonly Dictionary<string, object> _propertyValues;

        protected NotifyBase()
        {
           _propertyValues = new Dictionary<string, object>();
        }

        protected void Set<T>(T value, [CallerMemberName] string name = "")
        {
           if (_propertyValues.ContainsKey(name))
           {

               _propertyValues[name] = value;
               NotifyPropertyChanged(name);

           }
           else
           {
               _propertyValues.Add(name, value);
               NotifyPropertyChanged(name);
           }
        }

        protected T Get<T>([CallerMemberName] string name = "")
        {
           if (_propertyValues.ContainsKey(name))
           {
               return (T)_propertyValues[name];
           }
           return default(T);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

检查PropertyChanged.Fody NuGet

在编译时编织.NET程序集,使属性声明为

 public int Property { get; set; }

获取编译为

 private int property;
 public int Property 
 {
      get 
      {
        return property;
      }
      set
      {
        if(property != value)
        {
             property = value;
             if (this.PropertyChanged!= null) PropertyChanged(this, new PropertyChangedEventArgs("Property");
        }
      }
 }