如何使用反射来调用.NET中的超载方法
是否有一种方法可以使用.NET(2.0)中的反射调用过载的方法。我有一个应用程序,可以动态实例化已从公共基类得出的类。出于兼容目的,此基类包含2种同名方法,一种带有参数的方法,另一种没有参数。我需要通过Invoke方法调用无参数方法。现在,我所得到的只是一个错误,告诉我我正在尝试调用模棱两可的方法。
是的,我可以将对象作为我的基类实例施放,然后调用我需要的方法。最终,将发生,但是现在,内部并发症将不允许。
任何帮助都很棒!谢谢。
Is there a way to Invoke an overloaded method using reflection in .NET (2.0). I have an application that dynamically instantiates classes that have been derived from a common base class. For compatibility purposes, this base class contains 2 methods of the same name, one with parameters, and one without. I need to call the parameterless method via the Invoke method. Right now, all I get is an error telling me that I'm trying to call an ambiguous method.
Yes, I could just cast the object as an instance of my base class and call the method I need. Eventually that will happen, but right now, internal complications will not allow it.
Any help would be great! Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须指定要哪种方法:
You have to specify which method you want:
是的。当您调用该方法时,通过匹配所需过载的参数。
例如:
如果您以相反的方式执行此操作(即,通过找到会员Inlinfo并调用Invoke)请小心您获得正确的一个,则可以找到无参数的过载。
Yes. When you invoke the method pass the parameters that match the overload that you want.
For instance:
If you do this the other way round(i.e. by finding the MemberInfo and calling Invoke) be careful that you get the right one - the parameter-free overload could be the first found.
使用接收system.type []的getMethod过载,然后传递一个空型[];
Use the GetMethod overload that takes a System.Type[], and pass an empty Type[];