【发布时间】:2017-09-04 08:46:05
【问题描述】:
我有一个调用其他工具的验证方法:
public ValidationResult Validate(Some arg) {
var errors = new List<ValidationError>();
validate1(arg, errors);
if (errors.Count > 0) {
return ValidationResult.Failed(errors);
}
validate2(arg, other, errors);
if (errors.Count > 0) {
return ValidationResult.Failed(errors);
}
validate3(arg, other2, errors);
if (errors.Count > 0) {
return ValidationResult.Failed(errors);
}
return ValidationResult.Succeess();
}
我想要一些方法来制作如下代码,使用 for 循环来调用每个验证器:
public ValidationResult Validate(Some arg) {
var errors = new List<ValidationError>();
var validators = new [] {
validate1(arg, errors),
validate2(arg, other, errors),
validate3(arg, other2, errors)
};
foreach (var validator in validators) {
validator.invoke();
if (errors.Count > 0) {
return ValidationResult.Failed(errors);
}
}
return ValidationResult.Success();
}
我该怎么做?
【问题讨论】:
-
什么是
other和other2?validate有 2 个重载吗? -
使用数组或 lambda 列表。如果您以前没有使用过 Microsoft 的在线文档,您可以放心使用。
-
@Sweeper 没有重载,它是三种不同的方法,它们有不同的参数列表。