将委托添加到事件处理程序

发布于 2024-10-16 12:49:23 字数 345 浏览 3 评论 0原文

我正在尝试向 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆蝶 2024-10-23 12:49:23

现在无法测试代码,但是如果

this.Application.GetType().GetEvent("DocumentBeforeClose").AddEventHandler(this.Application,Delegate.CreateDelegate(ty, this, "test",false));

在 AddEventHandler 调用中使用 See this.Application 而不是 this 会怎么样?

更新:
现在我可以测试代码,如果将“this”更改为“this.Application”,正如我之前提到的,它可以正常工作,没有异常。这是完整的代码:

namespace WordTestAddin
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
          Type ty = this.Application.GetType().GetEvent("DocumentBeforeClose").EventHandlerType;
          var testDelegate = Delegate.CreateDelegate(ty, this, "test", false);
          this.Application.GetType().GetEvent("DocumentBeforeClose").AddEventHandler(this.Application, testDelegate);
        }

        void test(Word.Document Doc, ref bool Cancel)
        {
          System.Windows.Forms.MessageBox.Show("test");
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO generated code

        private void InternalStartup()
        {
          this.Startup += new System.EventHandler(ThisAddIn_Startup);
          this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}

因此请确保您的“测试”方法具有有效的签名。还要确保“test”是确切的方法名称,而不是“Test”或其他名称。

Can't test code right now, but what if use

this.Application.GetType().GetEvent("DocumentBeforeClose").AddEventHandler(this.Application,Delegate.CreateDelegate(ty, this, "test",false));

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:

namespace WordTestAddin
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
          Type ty = this.Application.GetType().GetEvent("DocumentBeforeClose").EventHandlerType;
          var testDelegate = Delegate.CreateDelegate(ty, this, "test", false);
          this.Application.GetType().GetEvent("DocumentBeforeClose").AddEventHandler(this.Application, testDelegate);
        }

        void test(Word.Document Doc, ref bool Cancel)
        {
          System.Windows.Forms.MessageBox.Show("test");
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO generated code

        private void InternalStartup()
        {
          this.Startup += new System.EventHandler(ThisAddIn_Startup);
          this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}

So ensure, that your "test" method has a valid signature. Also ensure, that "test" is the exact method name, not "Test" or something.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文