Attach event to dynamic object

前端 未结 3 475
情深已故
情深已故 2021-01-12 08:41

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

dynamic pdfCreator = Activator.CreateInstance(
                       Type.GetTypeFromProg         


        
相关标签:
3条回答
  • 2021-01-12 09:16

    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()
    {
    
    }
    
    0 讨论(0)
  • 2021-01-12 09:31

    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);
    
    0 讨论(0)
  • 2021-01-12 09:39

    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);
    
    0 讨论(0)
提交回复
热议问题