I would like to create my own method Assertsimilar to the code below, but it does not work.
Argument.Assert(() => Measurements.Count > 0);
public static void Assert(Expression<bool> expression)
{
bool value = expression.Compile();
if(!value)
{
throw new InvalidOperationException("Assert: " + expression.ToString() + " may not be false!");
}
}
What am I doing wrong here? Error: 'Error 1 Cannot convert lambda to an expression tree whose type argument 'bool' is not a delegate type'.
At first I had Expression<Func<bool>> expressionand expression.Compile()(), but it always fails with TargetInvocationException.
source
share