v11 v11 - 3 months ago 12
C# Question

How to use Func<> as parameter

I want to use the Func as parameter to delegate param, like below:

delegate IPic Creator( Node pnode );
int CalSize( Creator c );
Func<Node,Ipic> funcCreator = ... ;
//then I want to use calSize
calsize( funcCreator ); // error
calsize( funcCreator.invoke ); // error


Error message is the following:


Argument type 'System.Func<...Node, ...IPic>' is not assignable to parameter type '...Creator'.


So, as the code show, I want to use Func as params. How should I do?

Answer

You need to wrap your Func<> into a delegate:

(Creator)(pnode => funcCreator(pnode))

See this answer for the exact details.

Comments