在 Outlook VSTO 加载项中,C# 不允许您将事件处理程序挂接到 Application.Quit

In an Outlook VSTO add-in, C# won't let you hook an event handler to Application.Quit

微软文档说 (https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2010/ee720183(v=office.14)#practice-2-detecting-when-outlook-is-shutting-down)

To detect that Outlook is shutting down, you can use the Quit event of the Application object in the Outlook object model to receive a notification that the process is shutting down. Be sure that you respond quickly to the event and return control to Outlook as quickly as possible.

不过好像只能在VB.Net里做,C#不让。有一个名为 Quit() 的方法以及一个名为 Quit 的事件,当我尝试将事件处理程序连接到该事件时,C# 编译器说您不能在方法组上使用 +=。

顺便说一句,我试过了:你不能 create 任何在 C# 中具有相同名称的事件和方法。语言不允许。

下面的示例是 Outlook 插件的 VSTO 项目模板代码,仅添加了一个方法 QuitHandler,并尝试将其挂接到 Quit 事件。

当您输入代码时,intellisense 将同时显示 Quit 事件和 Quit 方法以供选择,但选择事件会导致代码无法编译。

有没有办法明确告诉编译器它是您打算使用的退出 事件

namespace OutlookAddIn2
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            Application.Quit += MyQuitHandler;
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            // Note: Outlook no longer raises this event. If you have code that 
            //    must run when Outlook shuts down, see https://go.microsoft.com/fwlink/?LinkId=506785
        }

        void MyQuitHandler()
        {

        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}

编译器输出(您可以忽略 en-US 警告,据我所知,我在 Visual Studio 的所有版本中都有这种情况,据我所知,这是由安装 Office 和 Visual Studio在同一台机器上。)

1>------ Build started: Project: OutlookAddIn2, Configuration: Debug Any CPU ------
1>CSC : warning CS2038: The language name 'en-US' is invalid.
1>D:\Temp\OutlookAddIn2\ThisAddIn.cs(7,10,7,26): error CS1656: Cannot assign to 'Quit' because it is a 'method group'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

当我在 VB.Net 中尝试相同时,问题不存在,编译成功:

Public Class ThisAddIn

    Private Sub ThisAddIn_Startup() Handles Me.Startup
        AddHandler Application.Quit, AddressOf MyQuitHandler
    End Sub

    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown

    End Sub

    Private Sub MyQuitHandler()

    End Sub

End Class

这是使用 Vusual Studio 2017 和 Resharper 2017.3.1。

我不认为 Resharper 在这种特殊情况下是罪魁祸首:当它的符号缓存变坏时,我看到了类似的错误消息,但这永远不会导致无法编译的代码。相反,它导致编译和工作的代码尽管代码编辑器指示的错误。

[编辑] 顺便说一句,它也不是这样工作的,我认为它是隐式的

Application.Quit += new ApplicationEvents_11_QuitEventHandler(MyQuitHandler);

No Application Quit event in Outlook?

重复

答案(我测试并确认有效):

((Outlook.ApplicationEvents_11_Event)Application).Quit 
+= new Outlook.ApplicationEvents_11_QuitEventHandler(ThisAddIn_Quit);

void ThisAddIn_Quit()
{
   System.Windows.Forms.MessageBox.Show("bye bye problem, I found the solution!!");
}