Event Invocation List

  • Thread starter Thread starter robinhoode
  • Start date Start date
R

robinhoode

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
 
For the invocation list .... MyEvent.GetInvocationList()
Here is a sample is taken from
[http://msdn.microsoft.com/msdnmag/issues/05/06/EventHandling/]

' From FileSearch4.vb
Dim ListenerList() As System.Delegate
Dim Listener As FileFoundEventHandler
ListenerList = FileFoundEvent.GetInvocationList()

' Search for matching file names.
Dim afi() As FileInfo = diLocal.GetFiles(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.AddRange(afi)
Next

I hope that is helpful.

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

swfObjectInstance.SomeEvent += new EventHandler(MyMethod);

then elsewhere:

Delegate[] invocationList =
swfObjectInstance.SomeEvent.GetInvocationList();

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
 
robinhoode said:
I was hoping to get the invocation list of an existing instance, after
the existing idiom:

swfObjectInstance.SomeEvent += new EventHandler(MyMethod);

then elsewhere:

Delegate[] invocationList =
swfObjectInstance.SomeEvent.GetInvocationList();

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.

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).
 
Back
Top