1

I'll blame tiredness though this one has really got the best of me

var result = Expression.Lambda<Func<T, bool>>(expression, parameterType).Compile();

With Error: Cannot implicitly convert type 'System.Func' to 'bool'

As you've probably guessed, I need this to return to a bool rather than Func

Thanks in advance

  • So you've written an expression that's compiled to a lambda. You need to invoke it now, like `result(someValueForT)`. – vcsjones Jun 13 '17 at 15:19
  • 2
    What is `expression`? – Ofir Winegarten Jun 13 '17 at 15:26
  • I guess you need to add (): ...`.Compile()(default(T))` returns a bool. But you need to explain better for people not to blindly guess. – Stefano d'Antonio Jun 13 '17 at 16:06
  • Sorry for the lack of information. I'm hoping to achieve something similar to this Rule Engine implementation as shown here https://stackoverflow.com/questions/6488034/how-to-implement-a-rule-engine though rather than just returning the result of the rule, I'd like to capture an additional property of the rule "Description". This would then return a list of RuleResults which would identify which rule failed and its description I can't find a way to Invoke the compiled the rule and still retain its description property –  Jun 19 '17 at 10:06

1 Answers1

0
public class Class1<T>
{
    public Class1()
    {
        var result = Expression.Lambda<Func<T, bool>>(null, new[] {new ParameterExpression()});
        var method = result.Compile();
        bool result = method(new T());
    }
}

This compiles perfectly fine for me.

Dbl
  • 5,634
  • 3
  • 41
  • 66