using NPOI.Util; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; namespace AllBase.Model.MvvmBaseClasses { public class PropertyNotifyObject : NotifyPropertyBase, IDisposable { public PropertyNotifyObject() { } Dictionary _ValueDictionary = new Dictionary(); #region 根据属性名得到属性值 public T GetPropertyValue(string propertyName) { if (string.IsNullOrEmpty(propertyName)) throw new ArgumentException("invalid property name: " + propertyName); object _propertyValue; if (!_ValueDictionary.TryGetValue(propertyName, out _propertyValue)) { _propertyValue = default(T); //_ValueDictionary.Add(propertyName, _propertyValue); _ValueDictionary[propertyName] = _propertyValue; } return (T)_propertyValue; } #endregion public void SetPropertyValue(string propertyName, T value) { if (!_ValueDictionary.ContainsKey(propertyName) || _ValueDictionary[propertyName] != (object)value) { _ValueDictionary[propertyName] = value; OnPropertyChanged(propertyName); } } #region Dispose public void Dispose() { DoDispose(); } ~PropertyNotifyObject() { DoDispose(); } void DoDispose() { if (_ValueDictionary != null) _ValueDictionary.Clear(); } #endregion } public static class PropertyNotifyObjectEx { public static U GetValue(this T t, Expression> exp) where T : PropertyNotifyObject { string _pN = MyCommMetoh.GetPropertyName(exp); return t.GetPropertyValue(_pN); } public static void SetValue(this T t, Expression> exp, U value) where T : PropertyNotifyObject { string _pN = MyCommMetoh.GetPropertyName(exp); t.SetPropertyValue(_pN, value); } } public class NotifyPropertyBase : INotifyPropertyChanged { #region INotifyPropertyChanged public void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } public event PropertyChangedEventHandler PropertyChanged; #endregion } public static class NotifyPropertyBaseEx { public static void OnPropertyChanged(this T npb, Expression> exp) where T : NotifyPropertyBase, new() { string _PropertyName = MyCommMetoh.GetPropertyName(exp); npb.OnPropertyChanged(_PropertyName); } } public class MyCommMetoh { //得到属性的名称 public static string GetPropertyName(Expression> exp) { string _pName = ""; if (exp.Body is MemberExpression) { _pName = (exp.Body as MemberExpression).Member.Name; } else if (exp.Body is UnaryExpression) { _pName = ((exp.Body as UnaryExpression).Operand as MemberExpression).Member.Name; } return _pName; } } }