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 ?
Like this (using a 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),
};
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With