当一个程序员有“洁癖”的时候,它的代码才会越写越好。
虽然不敢说自己水平有多高,但是自己写代码也越来越染上洁癖,
不允许自己的代码中有一丝敷衍或者是架构的别扭。
每个细节都要优美,考虑设计模式。每个逻辑都考虑如何复用,虽然代码写起来可能比较慢,但是出自自己手下的代码一定要是“精品”。
这里奉上网上一个兄弟提供的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(); } }