8

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?

Edward Brey
  • 40,302
  • 20
  • 199
  • 253
BennoDual
  • 5,865
  • 15
  • 67
  • 153

3 Answers3

8

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);
Edward Brey
  • 40,302
  • 20
  • 199
  • 253
  • suppose the event handler has arguments like this public void _PDFCreator_eReady(object sender , MyCustomArgument e){ } In this case what will be the event registration code? – Aneesh May 09 '14 at 06:29
  • 1
    @Aneesh: `pdfCreator.eReady += new Action(_PDFCreator_eReady);` – Edward Brey May 09 '14 at 13:16
5

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()
{

}
M.Babcock
  • 18,753
  • 6
  • 54
  • 84
  • Will the "pdfCreator.eReady = null;" clear the previously attached event? – TcKs Jan 01 '12 at 17:05
  • @TcKs - Most likely, but if you're going to set it again anyway then why would it matter? – M.Babcock Jan 01 '12 at 17:07
  • So this solution is good idea only during initialization not in further user, understand it I right?. – TcKs Jan 01 '12 at 17:15
  • @TcKs - Not necessarily. As I pointed out in my last comment, if you're overwriting the event anyway then why would it matter that you are clearing out the previously attached event before setting it? – M.Babcock Jan 01 '12 at 17:17
  • @M.Babcock - sorry, I have forgotten the dynamic-Keyword in my post while simplifying the code - I have changed it now. – BennoDual Jan 02 '12 at 15:16
  • @M.Babcock - when I try your solution, I get the Exception: System.__ComObject' does not contain a definition for 'eError'. But when I reference the COM-Object and cast pdfCreator to the right Interface, I can work with the event. But I want not reference the COM-Object. – BennoDual Jan 02 '12 at 15:18
  • @t.kehl - Has this helped you solve your problem or are you still looking for a solution? – M.Babcock Jan 04 '12 at 05:06
0

I ended up using following as other options did not work. You might have to use generic of < T > if your EventHandler is a generic

pdfCreator.eReady += new System.EventHandler(_PDFCreator_eReady);
Mandeep Janjua
  • 15,583
  • 4
  • 29
  • 24