Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
241 views
Welcome To Ask or Share your Answers For Others

1 Answer

The closest you can come is to use Delegate - the caller will need to pick the right delegate type. That's exactly why Control.Invoke uses Delegate instead of a specific delegate type.

Alternatively, there's MethodInfo as 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, then MethodInfo is 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...