c# 如何使用谓词执行多重验证和 return 错误消息
c# How to perform multiple validation and return error message with predicate
只看一个示例代码,其中调用了多个函数来执行验证,如果任何一个验证失败,则调用环境 return false 如果全部通过,则 return true。
如何自定义例程结果,当任何验证失败时,错误消息应为 return,当所有验证通过时,空字符串将为 return。查看我的代码并帮助我使用谓词自定义我想要的方式。
private static Predicate<string>[] _checks = new Predicate<string>[]
{
ValidationSeries.IsAtLeastFiveChars,
ValidationSeries.HasASpace,
ValidationSeries.HasNoLeadingSpace,
ValidationSeries.HasNoTrailingSpace
};
private void button1_Click(object sender, EventArgs e)
{
bool doesPassAllChecks = _checks.All(c => c("Hello Test"));
}
public static class ValidationSeries
{
public static bool IsAtLeastFiveChars(string text)
{
return text.Length >= 5;
}
public static bool HasASpace(string text)
{
return text.Contains(' ');
}
public static bool HasNoLeadingSpace(string text)
{
return !text.StartsWith(" ");
}
public static bool HasNoTrailingSpace(string text)
{
return !text.EndsWith(" ");
}
}
寻求帮助。谢谢
这样解决了。
public static class ValidationSeries
{
private static Predicate<string>[] _checks = new Predicate<string>[]
{
ValidationSeries.IsAtLeastFiveChars,
ValidationSeries.HasASpace,
ValidationSeries.HasNoLeadingSpace,
ValidationSeries.HasNoTrailingSpace
};
public static bool Check(string s, ICollection<string> failedPredicateNames)
{
var failed = false;
foreach (var check in _checks)
{
if ( !check(s) )
{
failedPredicateNames.Add(check.Method.Name);
failed = true;
}
}
return !failed;
}
public static bool IsAtLeastFiveChars(string text)
{
return text.Length >= 5;
}
public static bool HasASpace(string text)
{
return text.Contains(" ");
}
public static bool HasNoLeadingSpace(string text)
{
return !text.StartsWith(" ");
}
public static bool HasNoTrailingSpace(string text)
{
return !text.EndsWith(" ");
}
}
class Program
{
static void Main(string[] args)
{
var failures = new List<string>();
if (!ValidationSeries.Check(" Hello World", failures))
{
foreach (var f in failures)
{
Console.WriteLine(f);
}
}
}
只看一个示例代码,其中调用了多个函数来执行验证,如果任何一个验证失败,则调用环境 return false 如果全部通过,则 return true。
如何自定义例程结果,当任何验证失败时,错误消息应为 return,当所有验证通过时,空字符串将为 return。查看我的代码并帮助我使用谓词自定义我想要的方式。
private static Predicate<string>[] _checks = new Predicate<string>[]
{
ValidationSeries.IsAtLeastFiveChars,
ValidationSeries.HasASpace,
ValidationSeries.HasNoLeadingSpace,
ValidationSeries.HasNoTrailingSpace
};
private void button1_Click(object sender, EventArgs e)
{
bool doesPassAllChecks = _checks.All(c => c("Hello Test"));
}
public static class ValidationSeries
{
public static bool IsAtLeastFiveChars(string text)
{
return text.Length >= 5;
}
public static bool HasASpace(string text)
{
return text.Contains(' ');
}
public static bool HasNoLeadingSpace(string text)
{
return !text.StartsWith(" ");
}
public static bool HasNoTrailingSpace(string text)
{
return !text.EndsWith(" ");
}
}
寻求帮助。谢谢
这样解决了。
public static class ValidationSeries
{
private static Predicate<string>[] _checks = new Predicate<string>[]
{
ValidationSeries.IsAtLeastFiveChars,
ValidationSeries.HasASpace,
ValidationSeries.HasNoLeadingSpace,
ValidationSeries.HasNoTrailingSpace
};
public static bool Check(string s, ICollection<string> failedPredicateNames)
{
var failed = false;
foreach (var check in _checks)
{
if ( !check(s) )
{
failedPredicateNames.Add(check.Method.Name);
failed = true;
}
}
return !failed;
}
public static bool IsAtLeastFiveChars(string text)
{
return text.Length >= 5;
}
public static bool HasASpace(string text)
{
return text.Contains(" ");
}
public static bool HasNoLeadingSpace(string text)
{
return !text.StartsWith(" ");
}
public static bool HasNoTrailingSpace(string text)
{
return !text.EndsWith(" ");
}
}
class Program
{
static void Main(string[] args)
{
var failures = new List<string>();
if (!ValidationSeries.Check(" Hello World", failures))
{
foreach (var f in failures)
{
Console.WriteLine(f);
}
}
}