[英]Passing a condition into Func<bool> of a Tuple<string, string, Func<bool>>
I am trying to create a list of tuples with properties and conditions for their validation. 我正在尝试创建一个元组列表,其中包含用于验证的属性和条件。 So I had this idea in mind: 所以我有这个想法:
List<Tuple<string, string, Func<bool>>> properties = new List<Tuple<string,
string,
Func<bool>>>
{
Tuple.Create(FirstName, "User first name is required", ???),
};
...
How can I pass expression of type (FirstName == null) as Func ? 如何将类型(FirstName == null)的表达式作为Func传递?
Like this (using a lambda expression ): 像这样(使用lambda表达式 ):
var properties = new List<Tuple<string, string, Func<bool>>>
{
Tuple.Create<string, string, Func<bool>>(
FirstName,
"User first name is required",
() => FirstName == null),
};
Something like this: 像这样的东西:
List<Tuple<string, string, Func<bool>>> properties = new List<Tuple<string, string, Func<bool>>>
{
Tuple.Create(FirstName, "User first name is required", new Func<bool>(() => FirstName == null)),
};
Note that there are some limitatons to type inference for lambda expressions... for this reason the new Func<bool>
way of building a delegate is used. 请注意,对于lambda表达式的类型推断存在一些限制...因此,使用了new Func<bool>
构建委托的方法。
Alternatives: 备择方案:
Tuple.Create(FirstName, "User first name is required", (Func<bool>)(() => FirstName == null)),
Tuple.Create<string, string, Func<bool>>(FirstName, "User first name is required", () => FirstName == null),
new Tuple<string, string, Func<bool>>(FirstName, "User first name is required", () => FirstName == null),
In the end you have to repeate the Func<bool>
somewhere. 最后你必须在某处重复Func<bool>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.