By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,022 Members | 1,401 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,022 IT Pros & Developers. It's quick & easy.

Late binding and delegate method signatures

P: n/a
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_Workboo kBeforeC
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!
Nov 15 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.