http://blogs.wankuma.com/rti/archive/2009/04/22/171903.aspx
データに知能を持たせる
ってことは
これでもいいかな?
と思ったんだけど、どんなもんだろう?
こっちの方が汎用性が高いと思った訳ですよ。
public class Hoge : System.ComponentModel.INotifyPropertyChanged {
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
public Property<int> Code;
public Property<strng> Name;
public Property<int> Age;
public Hoge() {
this.Code = new Property<int>((o, n) => o != n, () => this.OnPropertyChanged("Code"));
this.Name = new Property<string>();
this.Age = new Property<int>((o, n) => n == 18);
}
private void OnPropertyChanged(string propertyName) {
if (this.PropertyChanged == null) return;
this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
public class Property<T> {
private Func<T, T, bool> f1 = null;
private Action f2 = null;
public Property() {}
public Property(Func<T, T, bool> func) {
this.f1 = func;
}
public Property(Action func) {
this.f2 = func;
}
public Property(Func<T, T, bool> func1, Action func2) {
this.f1 = func1;
this.f2 = func2;
}
private T _Value;
public T Value {
get {
return this._Value;
}
set {
if (f1 != null) {
if (!f1(this._Value, value)) return;
}
this._Value = value;
if (f2 != null) f2();
}
}
}