简体   繁体   中英

register eventhandler for Office 2013 event in vs 2010 by reflections in C#

I have an Outlook 2010 Add-In project written in C# using Visual Studio 2010.

Since the Add-In is overall working in Outlook 2013 I just want to make a slight modification, to prevent problems with the new InlineResponse feature in Outlook 2013.

I want to register an eventhandler for the InlineResponse event without upgrading to VS 2012 (because of the removed installer project). I read about using reflections to get the new events.

I don't get any exceptions, but the event doesn't trigger my handler (OnInlineResponse not called).

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");
   }
}

Any suggestions how I might achieve the desired behaviour?

Gavin Smyth (who wrote the mentioned post about using reflections) was so kind to give me an answer to the differences between our implementations:

from Gavin Smyth

The only difference I can spot between yours and mine is that OnInlineResponse takes an argument, the newly created mail item - see http://msdn.microsoft.com/en-us/library/office/jj229061 - ie, my method is defined as:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM