如何分解表达式以满足通用属性更改方法? [英] How to decompose expression to satisfy generic property change method?
问题描述
我有一个实现 INotifyPropertyChanged
的基础EF实体类。
I have a base EF entity class which implements INotifyPropertyChanged
.
基本属性Id是我的示例:
The base property, Id is my example:
/// <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:
...where SetValue is defined:
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));
}
}
我可以找到的所有样本都是期望的参数。我更喜欢setter(SetValue调用)尽可能的简单 - 即有没有办法将输入参数减少到1?
All samples I can find are expecting parameters. I prefer that the setter (SetValue call) is as simple as possible - i.e., is there a way to reduce the input parameter to 1?
推荐答案
您应该更改
var propertyName = ((PropertyExpression)expr.Left).Name;
至
var propertyName = ((MemberExpression)expr.Left).Member.Name;
您的代码编译,但您所做的是不是最佳和可信赖的。而你会得到一个 InvalidCastException
!
and your code compiles, but what you are doing is not optimal and trustful at all. And you'll get an InvalidCastException
!
编译一个 Expression< T>每个调用的code>并不是最佳的,你怎么知道用户将lambda传递给方法,如:
Compiling an Expression<T>
on every call is not optimal, and, how can you tell that the user passes the lambda to the method like:
() => (Id != value)
而不是
() => (id != value) // using the field instead of property
或
() => (value != Id) // passing the property as the second operand
?
另外,您的表达式中的值
不是 ConstantExpression
。 值
本身只是属性的集合
部分的局部变量,当传递给lambda表达式时,被提升为类字段(该值被捕获 - 请参阅此处更多信息)。那么你所拥有的是一个 MemberExpression
双方。
Also, value
in your expression is not a ConstantExpression
. The value
itself is just a local variable to the set
part of the property, and when passed to a lambda expression, is promoted to a class field (the value is captured - see here for more information). So what you have is a MemberExpression
on both sides.
我强烈建议您使用这种方法,如果你不能使用.NET 4.5( [CallerMemberName]
):
I highly recommend using this approach if you can't use .NET 4.5 ([CallerMemberName]
):
public class EntityBase : INotifyPropertyChanged
{
protected virtual void OnPropertyChanged(string propName)
{
var h = PropertyChanged;
if (h != null)
h(this, new PropertyChangedEventArgs(propName));
}
public event PropertyChangedEventHandler PropertyChanged;
protected bool ChangeAndNofity<T>(ref T field, T value, Expression<Func<T>> memberExpression)
{
if (memberExpression == null)
{
throw new ArgumentNullException("memberExpression");
}
var body = memberExpression.Body as MemberExpression;
if (body == null)
{
throw new ArgumentException("Lambda must return a property.");
}
if (EqualityComparer<T>.Default.Equals(field, value))
{
return false;
}
field = value;
OnPropertyChanged(body.Member.Name);
return true;
}
}
使用它很简单:
public class Person : EntityBase
{
private int _id;
public int Id
{
get { return _id; }
set { ChangeAndNofity(ref _id, value, () => Id); }
}
}
这篇关于如何分解表达式以满足通用属性更改方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!