潔癖的程式員_string

來源:互聯網
上載者:User

當一個程式員有“潔癖”的時候,它的代碼才會越寫越好。

雖然不敢說自己水平有多高,但是自己寫代碼也越來越染上潔癖,

不允許自己的代碼中有一絲敷衍或者是架構的彆扭。


每個細節都要優美,考慮設計模式。每個邏輯都考慮如何複用,雖然代碼寫起來可能比較慢,但是出自自己手下的代碼一定要是“精品”。


這裡奉上網上一個兄弟提供的WPF中資料繫結的屬性精簡寫法,


本來很長一段,如下。微軟官方代碼產生的例子

public string Password {            get {                return this.PasswordField;            }            set {                if ((object.ReferenceEquals(this.PasswordField, value) != true)) {                    this.PasswordField = value;                    this.RaisePropertyChanged("Password");                }            }        }

但是可以封裝基類,做到優雅的寫法

private DateTime _updateTime;        public DateTime UpdateTime        {            get { return _updateTime; }            set { _updateTime = value; this.NotifyPropertyChanged(p => p.UpdateTime); }        }

請看基類

    public class PropertyChangedBase : INotifyPropertyChanged    {        public event PropertyChangedEventHandler PropertyChanged;        public void NotifyPropertyChanged(string propertyName)        {            if (this.PropertyChanged != null)            {                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));            }        }    }    public static class PropertyChangedBaseEx    {        public static void NotifyPropertyChanged<T, TProperty>(this T propertyChangedBase, Expression<Func<T, TProperty>> expression) where T : PropertyChangedBase        {            var memberExpression = expression.Body as MemberExpression;            if (memberExpression != null)            {                string propertyName = memberExpression.Member.Name;                propertyChangedBase.NotifyPropertyChanged(propertyName);            }            else                throw new NotImplementedException();        }    }


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.