jeudi 4 février 2021

How to invoke with reflection a method that accepts a Func<> parameter?

When I try to add the Func<> parameter in the array of MethodInfo.Invoke, it's giving me the error that it can't convert a method group to an object.

How do I deal with this?

The method's signature:

static bool Something(Func<Expression, Expression, BinaryExpression> body)

What I'm passing:

MethodInfo.Invoke(null, new object[] { Expression.Subtract }); // compilation error

CS0428 Cannot convert method group 'Subtract' to non-delegate type 'object'. Did you intend to invoke the method?

Indeed it makes sense since Substract is not Func<...>. Is there better way than simple cast new object[] { (Func<Expression, Expression, BinaryExpression>)Expression.Subtract } ?





Aucun commentaire:

Enregistrer un commentaire