Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# FluentValidations从RuleBuilderOptions获取Propertyname_C#_Validation_Fluentvalidation - Fatal编程技术网

C# FluentValidations从RuleBuilderOptions获取Propertyname

C# FluentValidations从RuleBuilderOptions获取Propertyname,c#,validation,fluentvalidation,C#,Validation,Fluentvalidation,我试图创建一个自定义的When扩展来检查我的实体是否有更改。但是我在获取Propertyname时遇到了问题,我需要验证实例 public static bool WhenHasChanged<T, TProperty>(this IRuleBuilderOptions<T, TProperty> rule) { //I need to get the PropertyValidatorContext from the rule PropertyValid

我试图创建一个自定义的When扩展来检查我的实体是否有更改。但是我在获取Propertyname时遇到了问题,我需要验证实例

public static bool WhenHasChanged<T, TProperty>(this IRuleBuilderOptions<T, TProperty> rule)
{
    //I need to get the PropertyValidatorContext from the rule
    PropertyValidatorContext context;
    var instance = (IChangeTrackingObject)context.Instance;

    if (false == instance.GetChangedProperties().ContainsKey(context.PropertyName))
    {
        return true;
    }

    var oldValue = instance.GetChangedProperties().Get(context.PropertyName).OldValue;
    var newValue = context.PropertyValue;

    return (null == oldValue) ? null == newValue : oldValue.Equals(newValue);
}
更改时的公共静态bool(此IRuleBuilderOptions规则) { //我需要从规则中获取PropertyValidatorContext PropertyValidatorContext上下文; var instance=(IChangeTrackingObject)context.instance; if(false==instance.GetChangedProperties().ContainsKey(context.PropertyName)) { 返回true; } var oldValue=instance.GetChangedProperties().Get(context.PropertyName).oldValue; var newValue=context.PropertyValue; 返回值(null==oldValue)?null==newValue:oldValue.Equals(newValue); }
我需要获取正在验证的PropertyName和正在验证的实例,通常它们位于
PropertyValidatorContext
中。是否有方法从规则中获取
PropertyValidatorContext

我最终创建了一个必须的扩展,这样我就可以访问属性验证器上下文:

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);
    };
}
private static Func MustWhenChangedPredicate(Func谓词)
{
返回(t,p,context)=>
{
var instance=(IChangeTrackingObject)context.instance;
//类型名称始终作为属性的前缀
var propertyName=context.propertyName.Split(new[]{.'},2).Skip(1).First();
if(false==instance.GetChangedProperties().ContainsKey(propertyName))
{
返回true;
}
var oldValue=instance.GetChangedProperties().Get(propertyName).oldValue;
var newValue=context.PropertyValue;
if(oldValue==null&&newValue==null)
{
返回true;
}
if((oldValue!=null&&oldValue.Equals(newValue))||
(newValue!=null&&newValue.Equals(oldValue)))
{
返回true;
}
返回谓词(t,p,context);
};
}