Calling RaiseEvent over Reflection?

Hi,

I have a Event on my class and want to call RaiseEvent using Reflection. I don't want to use a workaround method which calls RaiseEvent for me ...

I tried calling my EventInfo's GetRaiseMethod but it simply return nothing. How can I do this

 

Thanks




Answer this question

Calling RaiseEvent over Reflection?

  • Nitin Unni

    I think that is the best available way at the moment. Let me check with a few folks here to understand if a better option is available and get back to you.

  • VeeBeeginnerr

    I'd like to do this too. But I'm confused about your solution. Given an arbitrary object passed in and the name of an event on that object, I'd like to raise the event. But how do I know what delegate that event will call


  • ali_kiran

    Thanks for your reply.

    So it is not possible in any way to raise a event declared with event keyword by reflection !



  • Mr. Les

    The problem is that you will have to know the method that raises the event.

    Public Event Abc As EventHandler

    Public Sub OnAbc(e As System.EventArgs)

    RaiseEvent Abc(Me, e)

    End Sub

    You can create a delegate for OnAbc and call Abc during runtime but you cannot raise the event without a method during runtime.



  • Xinwei Hong

    TheGetRaiseMethod returns null for all events declared with the event keyword as explained in the MSDN help for the same. The documentation apparently is not as verbose as it should have been and we will work on that.

    The GetRaiseMethod will return the method that was set through SetRaiseRaise method if you generate a method dynamically. If you need more help in this please let us know and I will walk you through an example where this will be useful.


    MSDN remarks for GetRaiseMethod:
    This method returns a null reference (Nothing in Visual Basic) for events declared with the C# event keyword or the Visual Basic Event keyword. This is because the C# and Visual Basic compilers do not generate such a method.

    Thottam R. Sriram

    Technical Program Manager

    CLR Team



  • soulz87

    I spoke with a few people here and it unfortunately is the solution that is available at this point. I will dig deeper into this and see what can be done in this area in V3 time frame. I will also post this on my blog to see what people think about this and what would be a good solution in the longer run.

  • assadgh

    That's the way I'm doing it atm.

    But I have to implement a OnEventName(e As EventArgs) Method for every event ...



  • TBone77

    The way you will probably do it is by delegates in C#. This would be a work around as opposed to getting the raisemethod and calling it during execution.

    You know what method to call and assign it to a delagate and invoke the delegate during run time.



  • Calling RaiseEvent over Reflection?