Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.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# 在自定义验证器中设置大小写不敏感_C#_Fluentvalidation_Ignore Case - Fatal编程技术网

C# 在自定义验证器中设置大小写不敏感

C# 在自定义验证器中设置大小写不敏感,c#,fluentvalidation,ignore-case,C#,Fluentvalidation,Ignore Case,我正在使用Fluent Validation编写自定义验证,检查属性的当前值是否包含以下任何值列表: IRuleBuilderOptions<T, TProperty> IsOfValue<T, TProperty> rule, params TProperty[] validOptions) { return rule .Must(validOptions.Contains) .WithMessage("Custom Error") } IR

我正在使用
Fluent Validation
编写自定义验证,检查属性的当前值是否包含以下任何值列表:

IRuleBuilderOptions<T, TProperty> IsOfValue<T, TProperty> rule, params TProperty[] validOptions)
{
   return rule
     .Must(validOptions.Contains)
     .WithMessage("Custom Error")
}
IRuleBuilderOptions IsOfValue规则,参数TProperty[]有效选项)
{
返回规则
.Must(validOptions.Contains)
.WithMessage(“自定义错误”)
}
我的问题是。。。如何更改
Must(validOptions.Contains)
以便在值为string类型时忽略大小写

对于不使用列表的变体,我可以很容易地做到这一点,但我无法理解在这里使用的逻辑


我知道我需要使用
StringComparer.IgnoreOrdinalCase
stringcomparation.IgnoreOrdinalCase
,具体取决于

您可以尝试检查
t属性
类型,如果您可以使用
字符串

string.Contains(x.ToString(),StringComparer.OrdinalIgnoreCase)
否则使用默认值

IRuleBuilderOptions<T, TProperty> IsOfValue<T, TProperty>(this IRuleBuilder<T, TProperty> rule, params TProperty[] validOptions)
{
    IRuleBuilderOptions<T, TProperty> result = rule
                 .Must(validOptions.Contains)
                 .WithMessage("Custom Error");

   if(typeof(TProperty) == typeof(string)){
       string[] vailds = validOptions as string[];
       result = rule
                .Must(x => vailds.Contains(x.ToString(),StringComparer.OrdinalIgnoreCase))
                .WithMessage("Custom Error");

   }

   return result;
}   
IRuleBuilderOptions IsOfValue(此IRuleBuilder规则,参数TProperty[]validOptions)
{
IRuleBuilderOptions结果=规则
.Must(validOptions.Contains)
.WithMessage(“自定义错误”);
if(typeof(TProperty)=typeof(string)){
字符串[]vailds=作为字符串[]的有效选项;
结果=规则
.Must(x=>vailds.Contains(x.ToString(),StringComparer.OrdinalIgnoreCase))
.WithMessage(“自定义错误”);
}
返回结果;
}   

您可以创建该泛型方法的专用版本,它将被调用:

public static IRuleBuilderOptions<T, TProperty> IsOfValue<T, TProperty>(this IRuleBuilder<T, TProperty> rule, params TProperty[] validOptions)
{
    return rule
        .Must(validOptions.Contains)
        .WithMessage("Custom Error");
}

public static IRuleBuilderOptions<T, string> IsOfValue<T>(this IRuleBuilder<T, string> rule, params string[] validOptions) => rule.IsOfValue(StringComparer.OrdinalIgnoreCase, validOptions);
public static IRuleBuilderOptions<T, string> IsOfValue<T>(this IRuleBuilder<T, string> rule, StringComparer comparer, params string[] validOptions)
{
    return rule
        .Must(x => validOptions.Contains(x, comparer))
        .WithMessage("Custom Error");
}
公共静态IRuleBuilderOptions IsOfValue(此IRuleBuilder规则,参数tpProperty[]validOptions)
{
返回规则
.Must(validOptions.Contains)
.WithMessage(“自定义错误”);
}
公共静态IRuleBuilderOptions IsOfValue(此IRuleBuilder规则,参数字符串[]validOptions)=>rule.IsOfValue(StringComparer.OrdinalIgnoreCase,validOptions);
公共静态IRuleBuilderOptions IsOfValue(此IRuleBuilder规则、StringComparer比较器、参数字符串[]有效选项)
{
返回规则
.Must(x=>validOptions.Contains(x,比较器))
.WithMessage(“自定义错误”);
}

该方法的完整签名是什么?@Xiaoy312我很抱歉,但我不明白你所说的“签名”是什么意思——这是你粘贴的代码的第一行;这是不完整的。@Xiaoy312该代码中唯一遗漏的是将
有效选项列表设置为错误消息字符串的行。否则,这就是完整的代码。没问题:)很乐意帮忙