zoukankan      html  css  js  c++  java
  • 菲佣WPF——3(关于NotifyObject重大bug修复)

    之前的NotifyObjec有问题。是多个属性同时对一个Object进行操作。

    新代码如下。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.ComponentModel;
    using System.Linq.Expressions;
    
    namespace WpfApplication4
    {
        public abstract class NotifyObject:INotifyPropertyChanged
        {
            #region << Field >>
            private Dictionary<string, object> cache = new Dictionary<string, object>();
            #endregion
    
            #region << Property >>
            public event PropertyChangedEventHandler PropertyChanged;
            #endregion
    
            #region << Method >>
            public T GET<T>(Expression<Func<T>> express)
            {
                return GetPropertyValue<T>(GetPropertyName<T>(express));
            }
    
            public void SET<T>(Expression<Func<T>> express, object obj)
            {
                SetPropertyValue(GetPropertyName<T>(express), obj);
            }
    
            private string GetPropertyName<T>(Expression<Func<T>> express)
            {
                var memExpress = (MemberExpression)express.Body;
    
                if (memExpress == null)
                    throw new Exception("The expression is valid");
    
                return memExpress.Member.Name;
            }
    
            private T GetPropertyValue<T>(string propertyName)
            {
                if (string.IsNullOrEmpty(propertyName))
                    return default(T);
                else
                {
                    if (cache.ContainsKey(propertyName))
                        return (T)cache[propertyName];
                    else
                        return default(T);
                }
            }
    
            private void SetPropertyValue(string propertyName, object obj)
            {
                if (cache.ContainsKey(propertyName))
                {
                    if (!Object.ReferenceEquals(cache[propertyName], obj))
                    {
                        cache[propertyName] = obj;
                        Notify(propertyName);
                    }
                }
                else
                {
                    cache.Add(propertyName, obj);
                    Notify(propertyName);
                }
            }
    
            private void Notify(string propertyName)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
            #endregion
        }
    }
  • 相关阅读:
    linux free
    uptime
    简述负载均衡&CDN技术(转)
    大胆地去做自己坚信的事情,去做不伤害国家和客户的事情 做企业一定要专注。为企业制定战略目标,绝对不能超过三个。超过三个,你就记不住了,员工也记不住
    同一路由器不同vlan之间的通信(一)
    计算机基础之计算机网络与安全
    LayoutInflater的使用
    插入排序
    Java NIO与IO
    高速排序算法
  • 原文地址:https://www.cnblogs.com/qiurideyun/p/2912772.html
Copyright © 2011-2022 走看看