你能解释一下这段奇怪的代码吗?
expression.Compile()();
Run Code Online (Sandbox Code Playgroud)
为什么这里有两对括号?我没有在谷歌找到任何东西.完整的方法是
public Validator NotEmpty(Expression<Func<IEnumerable<T>>> expression)
{
var member = (MemberExpression)expression.Body;
string propertyName = member.Member.Name;
IEnumerable<T> value = expression.Compile()();
if (value == null || !value.Any())
{
ValidationResult.AddError(propertyName, "Shouldn't be empty");
}
return this;
}
Run Code Online (Sandbox Code Playgroud)
它是这样使用的:
_validator.NotEmpty(() => request.PersonIds); // request.PersonIds is List<int>
Run Code Online (Sandbox Code Playgroud)
此方法检查集合是空还是空.一切正常,但我对该代码有点困惑.我以前从未见过在C#中使用过两对括号.这是什么意思?