寄存器事件处理程序的Office 2013的事件在VS 2010的反射

问题描述:

我使用Visual Studio 2010寄存器事件处理程序的Office 2013的事件在VS 2010的反射

用C#编写一个Outlook 2010外接项目由于加载项是Outlook 2013中我只是整体工作希望稍作修改,以防止Outlook 2013中新的InlineResponse功能出现问题。

我想为InlineResponse事件注册事件处理程序,而无需升级到VS 2012(因为已删除的安装程序项目)。我读了关于使用reflections来获得新的事件。

我没有得到任何异常,但事件不会触发我的处理程序(OnInlineResponse未调用)。

public partial class ThisAddIn 
{ 
    Outlook.Explorer _explorer; 

    private void ThisAddIn_Startup(object sender, System.EventArgs e) 
    { 
     _explorer = Application.ActiveExplorer(); 

     AddInlineResponseHandler(); 
    } 

    private void AddInlineResponseHandler() 
    { 
     var einfo = _explorer.GetType().GetEvent("InlineResponse", BindingFlags.Public | BindingFlags.Instance); 

     if (einfo != null) 
     { 
     var handler = Delegate.CreateDelegate(einfo.EventHandlerType, this, this.GetType().GetMethod("OnInlineResponse", BindingFlags.NonPublic | BindingFlags.Instance), false); 

     einfo.AddEventHandler(_explorer, handler); 
     } 

    } 

    private void OnInlineResponse() 
    { 
     System.Windows.Forms.MessageBox.Show("InlineResponse"); 
    } 
} 

任何建议如何我可能实现所需的行为?

加文·史密斯(谁写了关于使用反射所提到的文章)是如此的亲切给我一个答案,我们的实现之间的差异:

from Gavin Smyth

唯一的区别我可以你们之间发现而我是OnInlineResponse需要一个参数,新创建的邮件项目 - 见http://msdn.microsoft.com/en-us/library/office/jj229061 - 也就是说,我的方法是定义为:

private void OnInlineResponse(object item) 
{ 
    ... 
}