Late binding and delegate method signatures

M

Matt

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.Office.Interop.Excel.Workbook,bool&)

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

// Get type for the close event handler
Type tDel = eApp.Assembly.GetType
("Microsoft.Office.Interop.Excel.AppEvents_WorkbookBeforeC
loseEventHandler",false,true);

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

// Register the handler
EventInfo ei = tExcel.GetEvent("WorkbookBeforeClose");
ei.AddEventHandler(eApp,myDel);
}

private void myHandler(object 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.Office.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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top