I need to call a method on an object but I do not know the method name until runtime.
What are the techniques available?
(eg GetMethod().Invoke(), delegates, c# 4.0 dynamic)
Thanks!
The C# 4.0 dynamic
functionality is going to be the easiest way to do this. In a very real sense, dynamic
is "just a wrapper" around Reflection. It's a very good wrapper, though, that is probably your best option.
Other ways, in approximately increasing level of difficult:
The last three are not for the faint of heart. Your best bet is to use dynamic
or write your own Reflection code. If I had had dynamic
three years ago when I was writing code for something similar, I would have used it.
You can use Reflection to call functions that you do not know the name of until run time. Here's some doc:
http://msdn.microsoft.com/en-us/library/f7ykdhsy%28VS.80%29.aspx
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.