I am implementing a very simple rules engine that enforces some specifications dynamically at runtime.
The actual rules are stored in Tuples, and I have trouble storing a delegate to the string.EndsWith
function.
The following code works for testing string equality, and returns False
as expected ("A"
is different from "B"
):
var rule = new Tuple<string, Func<string, string, bool>, string>("A", string.Equals, "B");
Console.WriteLine(rule.Item2.Invoke(rule.Item1, rule.Item3));
However, I cannot figure out how to adapt this code to use the string.EndsWith
function instead of string.Equals
.
The following code does not compile and issues a Cannot convert from 'method group' to 'Func<string, string, bool>'
error message in Visual Studio.
var rule = new Tuple<string, Func<string, string, bool>, string>("A", string.EndsWith, "B");
Console.WriteLine(rule.Item2.Invoke(rule.Item1, rule.Item3));
I did search before asking this question, but I cannot understand the answers provided in How do I fix 'compiler error - cannot convert from method group to System.Delegate'? or Cannot convert from method group to System.Func<string>. I do not see how to apply these to my problem.
String.Equals and String.EndsWith have different method signatures and must be called differently.
Specifically, String.Equals is static and takes two strings and returns a bool. String.EndsWith is an instance method taking one string and returns a bool
You can resolve your issue by wrapping the String.EndsWith call in a lambda to change the signature to take two strings and return a bool:
var rule = new Tuple<string, Func<string, string, bool>, string>
("AB", (string a, string b) => a.EndsWith(b), "B");
Console.WriteLine(rule.Item2.Invoke(rule.Item1, rule.Item3));
In general, the error means that there is no way the compiler can interpret string.EndsWith
as a Func<string, string, bool>
. You may find this answer about what is a method group helpful to understanding the error message.
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