Event Invocation List

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • robinhoode

    #1

    Event Invocation List

    I'm trying to serialize my menubar for use later, however I want to use
    XML to serialize this list. Not a problem with the Text property of the
    MenuItem control, however the ClickEvent is a bit trickier. I managed
    to find some code in the MSDN on getting a class method (and therefore
    a delegate) using a string, making deserialization possibe, however
    turning an event's evocation list into a string/array of strings
    doesn't seem as easy. I'd have to get the invocation list of the event,
    and from the looks of it's not possible (compiler gives an error: The
    event can only appear on the left hand side of += or -=). Anyone have
    any ideas?

    Thanks in advance

    -Rob

  • bobbychopra@gmail.com

    #2
    Re: Event Invocation List

    For the invocation list .... MyEvent.GetInvo cationList()
    Here is a sample is taken from
    [http://msdn.microsoft.com/msdnmag/is...ventHandling/]

    ' From FileSearch4.vb
    Dim ListenerList() As System.Delegate
    Dim Listener As FileFoundEventH andler
    ListenerList = FileFoundEvent. GetInvocationLi st()

    ' Search for matching file names.
    Dim afi() As FileInfo = diLocal.GetFile s(Me.FileSpec)

    For Each fi As FileInfo In afi
    For Each Listener In ListenerList
    Try
    Listener.Invoke (fi)
    Catch
    ' Something goes wrong? Just move on to
    ' the next event handler.
    End Try
    Next
    alFiles.AddRang e(afi)
    Next

    I hope that is helpful.

    Sincerely,
    Bobby

    Comment

    • robinhoode

      #3
      Re: Event Invocation List

      I was hoping to get the invocation list of an existing instance, after
      the existing idiom:

      swfObjectInstan ce.SomeEvent += new EventHandler(My Method);

      then elsewhere:

      Delegate[] invocationList =
      swfObjectInstan ce.SomeEvent.Ge tInvocationList ();

      But this raises an error of CS0079.

      Well.. I did a bit more research and it doesn't seem possible unless I:

      1) Do what you've shown here, by creating my own EventHandler and
      passing that on to the object
      2) Deriving the class and handling it internally

      If anyone has any alternatives, I'll be glad to hear them.

      -Rob

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Event Invocation List

        robinhoode <robinhoode@gma il.com> wrote:[color=blue]
        > I was hoping to get the invocation list of an existing instance, after
        > the existing idiom:
        >
        > swfObjectInstan ce.SomeEvent += new EventHandler(My Method);
        >
        > then elsewhere:
        >
        > Delegate[] invocationList =
        > swfObjectInstan ce.SomeEvent.Ge tInvocationList ();
        >
        > But this raises an error of CS0079.
        >
        > Well.. I did a bit more research and it doesn't seem possible unless I:
        >
        > 1) Do what you've shown here, by creating my own EventHandler and
        > passing that on to the object
        > 2) Deriving the class and handling it internally
        >
        > If anyone has any alternatives, I'll be glad to hear them.[/color]

        No - the point of the encapsulation of an event is that all you can do
        is subscribe and unsubscribe. You don't get to manipulate the list in
        any other way, unless the owner chooses to expose the delegate to you
        in another way.

        For field-like events you *could* use reflection to get the delegate,
        but not all events are declared in a field-like way (i.e. they're not
        all backed by a field of the same name).

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        Working...