[英]What the various ways of calling a method at runtime?
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) (例如,GetMethod()。Invoke(),委托,C#4.0动态)
Thanks! 谢谢!
The C# 4.0 dynamic
functionality is going to be the easiest way to do this. C#4.0
dynamic
功能将是最简单的方法。 In a very real sense, dynamic
is "just a wrapper" around Reflection. 在一个非常真实的意义上,
dynamic
是反射的“只是一个包装”。 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. 最好的选择是使用
dynamic
代码或编写自己的反射代码。 If I had had dynamic
three years ago when I was writing code for something similar, I would have used it. 如果我在三年前编写类似的代码时有
dynamic
,我会用它。
You can use Reflection to call functions that you do not know the name of until run time. 您可以使用Reflection来调用您不知道运行时名称的函数。 Here's some doc:
这是一些文档:
http://msdn.microsoft.com/en-us/library/f7ykdhsy%28VS.80%29.aspx http://msdn.microsoft.com/en-us/library/f7ykdhsy%28VS.80%29.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.