1 回答
![?](http://img1.sycdn.imooc.com/54586870000183e302200220-100-100.jpg)
TA贡献1862条经验 获得超7个赞
我最终创建了一个 must 扩展,所以我可以访问属性验证器上下文:
private static Func<T, TProperty, PropertyValidatorContext, bool> MustWhenChangedPredicate<T, TProperty>(Func<T, TProperty, PropertyValidatorContext, bool> predicate)
{
return (t, p, context) =>
{
var instance = (IChangeTrackingObject)context.Instance;
//The type name always prefixes the property
var propertyName = context.PropertyName.Split(new[] { '.' }, 2).Skip(1).First();
if (false == instance.GetChangedProperties().ContainsKey(propertyName))
{
return true;
}
var oldValue = instance.GetChangedProperties().Get(propertyName).OldValue;
var newValue = context.PropertyValue;
if (oldValue == null && newValue == null)
{
return true;
}
if ((oldValue != null && oldValue.Equals(newValue)) ||
(newValue != null && newValue.Equals(oldValue)))
{
return true;
}
return predicate(t, p, context);
};
}
- 1 回答
- 0 关注
- 185 浏览
添加回答
举报