将条件传递给 Tuple<string, string, Func<bool>> 的 Func<bool>
Passing a condition into Func<bool> of a Tuple<string, string, Func<bool>>
我正在尝试创建一个元组列表,其中包含用于验证的属性和条件。所以我想到了这个想法:
List<Tuple<string, string, Func<bool>>> properties = new List<Tuple<string,
string,
Func<bool>>>
{
Tuple.Create(FirstName, "User first name is required", ???),
};
...
如何将 (FirstName == null) 类型的表达式作为 Func 传递?
像这样(使用 lambda expression):
var properties = new List<Tuple<string, string, Func<bool>>>
{
Tuple.Create<string, string, Func<bool>>(
FirstName,
"User first name is required",
() => FirstName == null),
};
像这样:
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)),
};
请注意,lambda 表达式的类型推断存在一些限制...因此,使用 new Func<bool>
构建委托的方式。
备选方案:
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),
最后你必须在某处重复Func<bool>
。
我正在尝试创建一个元组列表,其中包含用于验证的属性和条件。所以我想到了这个想法:
List<Tuple<string, string, Func<bool>>> properties = new List<Tuple<string,
string,
Func<bool>>>
{
Tuple.Create(FirstName, "User first name is required", ???),
};
...
如何将 (FirstName == null) 类型的表达式作为 Func 传递?
像这样(使用 lambda expression):
var properties = new List<Tuple<string, string, Func<bool>>>
{
Tuple.Create<string, string, Func<bool>>(
FirstName,
"User first name is required",
() => FirstName == null),
};
像这样:
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)),
};
请注意,lambda 表达式的类型推断存在一些限制...因此,使用 new Func<bool>
构建委托的方式。
备选方案:
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),
最后你必须在某处重复Func<bool>
。