Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attach event to dynamic object

I create a c# dynamic object of a COM-Object on the fallowing way:

dynamic pdfCreator = Activator.CreateInstance(
                       Type.GetTypeFromProgID("PDFCreator.clsPDFCreator"));

The class clsPDFCreator is defining an event calling eReady. But when I try to register an Eventhandler like

pdfCreator.eReady += _PDFCreator_eReady;

I get the error message "Operator '+=' cannot be applied to operands of type 'dynamic' and 'method group'".

How can i register an EventHandler to an Event which is declared of a dynamic object?

like image 823
BennoDual Avatar asked Jan 01 '12 16:01

BennoDual


2 Answers

Since the delegate type is not known at compile time, you have to specify it. The Action delegate matches methods with no parameters or return value:

pdfCreator.eReady += new Action(_PDFCreator_eReady);
like image 155
Edward Brey Avatar answered Oct 14 '22 23:10

Edward Brey


How about this:

public delegate void eReadyHandler();

static void Main(string[] args)
{
    var comType = Type.GetTypeFromProgID("PDFCreator.clsPDFCreator");
    dynamic pdfCreator = Activator.CreateInstance(comType);
    //dynamic pdfCreator = new PDFCreator.clsPDFCreator();

    //pdfCreator.eReady = null;
    pdfCreator.eReady += new eReadyHandler(_PDFCreator_eReady);
}

public static void _PDFCreator_eReady()
{

}
like image 39
M.Babcock Avatar answered Oct 14 '22 23:10

M.Babcock