Late binding and delegate method signatures

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matt

    Late binding and delegate method signatures

    I'm trying to use late binding to automate Excel from C#
    so as to run with multiple versions of Excel, and am
    hoping to avoid referencing any Excel PIA in my project.
    But I need to add a C# event handler to do some processing
    before Excel closes. Ildasm shows the handler Invoke
    like this:

    void(class Microsoft.Offic e.Interop.Excel .Workbook,bool& )

    My current attempt at adding a late-bound event
    handler looks like this:
    {
    ....
    //Startup Excel
    Type tExcel = Type.GetTypeFro mProgID("Excel. Application");
    eApp = Activator.Creat eInstance(t);

    // Get type for the close event handler
    Type tDel = eApp.Assembly.G etType
    ("Microsoft.Off ice.Interop.Exc el.AppEvents_Wo rkbookBeforeC
    loseEventHandle r",false,tru e);

    // Create a delegate for the event
    Delegate myDel = Delegate.Create Delegate
    (tDel,this,"myH andler",true);

    // Register the handler
    EventInfo ei = tExcel.GetEvent ("WorkbookBefor eClose");
    ei.AddEventHand ler(eApp,myDel) ;
    }

    private void myHandler(objec t workbook, ref bool Cancel) {
    // Do processing here when workbook is closing
    ....
    }

    The CreateDelegate fails with an error
    "error binding to target method". I'm presuming that's
    because it expects a
    Microsoft.Offic e.Interop.Excel .Workbook parameter and not
    a generic object as I have. Is there any way in C# to
    have the CreateDelegate accept a method with a generic
    object parameter instead of the Excel.Workbook class?

    Thanks for any help!


Working...