none
Reflection and event handlers RRS feed

  • General discussion

  • Hi

    I am trying to use reflection to get all the events in word and then create a delegate that I can add to one of these events. The code I have so far is:

    Word.ApplicationClass wordApp = (Word.ApplicationClass)this.Application;
    EventInfo result = (from o in f
                    where o.Name == "DocumentBeforeClose"             
                 select o).FirstOrDefault();
    Type tDelegate = result.EventHandlerType;
    

    Is it possible to dynamically create an eventhandler of the same type as tDelegate which could then call the code I wish to execute??

    Thanks

    Monday, December 27, 2010 12:28 AM

All replies

  • Hello beats,

    Thanks for posting. As far as I know, we could not create a customized delegate. What kind of event do you want to use? Please check Word object model: http://msdn.microsoft.com/en-us/library/kw65a0we.aspx. If the event is not included in Word object model, it is impossible for us to create it.

    If you have any concern on this post, just feel free to follow up. Have a nice day.


    Bessie Zhao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 27, 2010 8:28 AM
  • Hi Bessie

    Thanks for the response.

    Well the idea is to pass the name of an event such as DocumentBeforeSave to a method a bit like:

    EventInfo p = getEvent(this.Application, "DocumentBeforeSave");
    

    public EventInfo getEvent(Word.Application wordApp, string eventName)
        {
          Type wordType = wordApp.GetType();
    
          EventInfo[] f = wordApp.GetType().GetEvents();
          EventInfo result = (from o in f
                    where o.Name == eventName
                    select o).FirstOrDefault();
          return result;
        }
    

    Now this gives me an EventInfo of Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeSaveEventHandler. Which to me looks like Word.ApplicationEvents4_DocumentBeforePrintEventHandler which can assign and eventhandler with +=;

    I see that the EventInfo has an AddEventHandler method. I am hoping that I can attach my own delegate to handle when the DocumentBeforeSave event fires.

    The problem is, I just don't seem to be able to get the delegate right. I have been playing around with this:

    MethodInfo[] myArrayMethodInfo = msw.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
          MethodInfo r = (from o in myArrayMethodInfo
                  where o.Name == "add_" + p.Name
                  select o).FirstOrDefault();
         
          Delegate del = Delegate.CreateDelegate(p.EventHandlerType,r, false);
    

    But the Delegate is always null. Is this just not possible or am I just doing it wrong.

    Thanks

    Monday, December 27, 2010 5:56 PM
  • Hello again beats,

    Sorry for delay. Based on my experience, Word object library is a COM component. It means we only could use it not customize it. From what you said, what you want to do is to attach a new delegate for DocumentBeforeSave event. Sorry, it is also impossible.

    If you have any concern on this post, or I have mistaken this issue, just feel free to point me out. Have a nice weekend.


    Bessie Zhao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, December 31, 2010 9:29 AM
  • Hi Bessie

    Thanks for the response.

    I don't think I have explained myself correctly and apologize for that. I don't want to customize the Word object library (sounds like voodoo madness if it was even possible). I just want to dynamically create an eventhandler of the same type, parameters etc.

    I was looking for a way to enumerate over all the events using relection and then just subscribe to the event I was interesting in. I may be wrong but a lot of the events have the same signature and so I didn't really want to write the same code over and over again, just changing the .

     

    public static IObservable<Word.Document> WhenDocumentSaving(this Word.Application application)
    		{
    			return Observable.Create<Word.Document>(observer =>
    			{
                // Subsitute handler here				<br/>
                Word.ApplicationEvents4_DocumentBeforeSaveEventHandler handler = <br/>
                    (Word.Document doc, ref bool saveAsUI, ref bool cancel) =>
    					{
    						observer.OnNext(doc);
    					};
    
    				application.DocumentBeforeSave += handler;
    
    				return () => application.DocumentBeforeSave -= handler;
    			});
    		}
    
    

    Dave Sexton has advised that it may be possible to use the below example to achieve this but I have not had the chance to experiment yet:

    http://social.msdn.microsoft.com/Forums/en/rx/thread/4b173cf8-8908-4a55-877f-0b4635f23bab

    I hope that makes more sense. I think I may have the terminology all confused.

    Thanks

     

    Friday, December 31, 2010 11:51 AM
  • Hi Beats,

    Could you please post your question to .Net developer forum (http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/threads).  Your question is a general question about creating an eventhandler dynamically.

    Thanks,

     

     


    Shiv Khare
    Wednesday, January 19, 2011 9:13 PM
    Moderator
  • Thanks Shiv

    Will do that now.

    Regards

     

    Thursday, January 20, 2011 11:27 PM