Is there a way to pass a method as a parameter without knowing anything about the method in advance?
Can Func<input, return> be called without knowing the method’s parameter/return type?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The closest you can come is to use
Delegate– the caller will need to pick the right delegate type. That’s exactly whyControl.InvokeusesDelegateinstead of a specific delegate type.Alternatively, there’s
MethodInfoas HackedByChinese suggests. Which one you should use depends on what you’re trying to achieve – each is useful in different contexts. If you’re trying to describe a method, thenMethodInfois probably the best approach. If you’re trying to represent a callable action of some kind (which can take parameters and have a return value) then a delegate is probably more useful.If you tell us more about what you’re trying to achieve, we may be able to help you more.