实例上的 Type.InvokeMember
如何在实例上调用 InvokeMember?
例如,如果我想使用实例的参数 "argument string"
调用方法 MyMethod()
:
MyObject myInstance = MyObject.GetObject();
// ?
我已经尝试过:
myInstance.GetType().InvokeMember("MyMethod",
BindingFlags.InvokeMethod | BindingFlags.Public,
null,
null,
new Object[] { "argument string" })
但它不起作用,抱怨 MyObject 没有 MyMethod,实际上它是一个实例方法,而不是一个类方法,所以我认为它试图将它作为静态方法调用。
有什么帮助吗?
How can I call InvokeMember on an instance?
For example, if I want to call the method MyMethod()
with the argument "argument string"
of an instance:
MyObject myInstance = MyObject.GetObject();
// ?
I've tried this:
myInstance.GetType().InvokeMember("MyMethod",
BindingFlags.InvokeMethod | BindingFlags.Public,
null,
null,
new Object[] { "argument string" })
but it does not work, complaining that MyObject doesn't have MyMethod, and indeed it is an instance method, not a class method, so I think it's trying to invoke it as a static method.
Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的第二个
null
应该是调用的目标:请参阅 有关更多详细信息,请参阅文档。
Your second
null
should be the target of the invocation:See the documentation for more details.
怎么样:
How about: