c#使用INotifyPropertyChange在类中压缩冗余代码

本文关键字:压缩 冗余 代码 使用 INotifyPropertyChange | 更新日期: 2023-09-27 18:18:39

在过去的几天里,我一直在阅读一些关于继承类和创建基类的文章,我经常在自己编写的工具中这样做。然而,我特别在寻找减少冗余代码的方法,这些代码经常写在包含INotifyPropertyChange的类中。通常我的类看起来像这样,继承NotifyBase的基类。然而,我在各种脚本中看到人们将一些Get和Set代码移到基类中。我想知道做这件事的时候要注意什么?这是坏习惯还是好习惯?我提供的示例是否正确地写到了这一点?

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

当前设置示例

<<p> FileItem类/em>
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));
            }
        }
    }
}

可能的新设置

<<p> FileItem类/em>
using System.IO;
namespace WpfApplication1
{
    public class FileItem : NotifyBase
    {
        public string FullPath
        {
            get { return Get<string>(); }
            set { Set(value); }
        }
    }
}

基类'NotifyBase '

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);
        }
    }
}

c#使用INotifyPropertyChange在类中压缩冗余代码

查看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");
        }
      }
 }