将委托添加到事件处理程序
我正在尝试向 Word 中的 EventHandler 添加委托,但在使用以下代码时不断收到参数异常:
Type ty = this.Aplication.GetType().GetEvent("DocumentBeforeClose").EventHandlerType;
this.Application.GetType().GetEvent("DocumentBeforeClose").AddEventHandler(this,Delegate.CreateDelegate(ty, this, "test",false));
测试只是弹出一个消息框。
有谁知道为什么会发生这种情况。
I am trying to add a delegate to an EventHandler in word but keep getting an Argument Exception while using the below code:
Type ty = this.Aplication.GetType().GetEvent("DocumentBeforeClose").EventHandlerType;
this.Application.GetType().GetEvent("DocumentBeforeClose").AddEventHandler(this,Delegate.CreateDelegate(ty, this, "test",false));
test just pops up a messagebox.
Does anyone know why this is happening.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在无法测试代码,但是如果
在 AddEventHandler 调用中使用 See this.Application 而不是 this 会怎么样?
更新:
现在我可以测试代码,如果将“this”更改为“this.Application”,正如我之前提到的,它可以正常工作,没有异常。这是完整的代码:
因此请确保您的“测试”方法具有有效的签名。还要确保“test”是确切的方法名称,而不是“Test”或其他名称。
Can't test code right now, but what if use
See this.Application instead of this in AddEventHandler call.
UPDATE:
Now I can test the code and it works fine without exceptions if change "this" to "this.Application" as I mentioned before. Here is a full code:
So ensure, that your "test" method has a valid signature. Also ensure, that "test" is the exact method name, not "Test" or something.