を実装する基本 EF エンティティ クラスがありますINotifyPropertyChanged
。
基本プロパティ Id は私の例です:
/// <summary>
/// Entity Id
/// </summary>
public int Id {
get { return id; }
set { SetValue<int>(() => (Id != value), (v) => id = v); } // < can this be simplified into a single call?
}
... SetValue が定義されている場所:
protected void SetValue<TValue>(Expression<Func<bool>> evalExpr, Action<TValue> set) {
// Compile() returns a Func<bool>
var doSetValue = evalExpr.Compile();
if (doSetValue()) {
var expr = evalExpr.Body as BinaryExpression;
// this is not compiling - how do I decompose the expression to get what I need?
var propertyName = ((PropertyExpression)expr.Left).Name;
var assignValue = (TValue)((ConstantExpression)expr.Right).Value;
set(assignValue);
_propertyChangedHandler(this, new PropertyChangedEventArgs(propertyName));
}
}
私が見つけることができるすべてのサンプルは、パラメーターを期待しています。セッター (SetValue 呼び出し) はできるだけ単純なものを好みます。つまり、入力パラメーターを 1 に減らす方法はありますか?