EventHandler<MyGenericEventArgs<MyObject>> problem

Topics: CAB & Smart Client Software Factory
Oct 5, 2007 at 9:10 AM
Edited Oct 5, 2007 at 9:31 AM
Hello All,

I've declared multiple event publication in different places
[EventPublication(@"MyTopic", PublicationScope.WorkItem)]
public event EventHandler<MyGenericEventArgs<MyObject1>> OnMyEventTaken;

[EventPublication(@"MyTopic", PublicationScope.WorkItem)]
public event EventHandler<MyGenericEventArgs<MyObject2>> OnMyEventTaken;

[EventPublication(@"MyTopic", PublicationScope.WorkItem)]
public event EventHandler<MyGenericEventArgs<MyObjectX>> OnMyEventTaken;

etc...

and multiplie event subscription
[EventSubscription(@"MyTopic", ThreadOption.Publisher)]
public void OnOnMyEventTaken(object sender, MyGenericEventArgs<MyObject1> eventArgs)

[EventSubscription(@"MyTopic", ThreadOption.Publisher)]
public void OnOnMyEventTaken(object sender, MyGenericEventArgs<MyObject2> eventArgs)

[EventSubscription(@"MyTopic", ThreadOption.Publisher)]
public void OnOnMyEventTaken(object sender, MyGenericEventArgs<MyObjectX> eventArgs)

etc...

I have 2 views. One parent and second child, in both these couple of events are declared, for example
View1 (child for Main window application, Main window application is parent for View1)
[EventPublication(@"MyTopic", PublicationScope.WorkItem)] // <- publication for parent main window application
public event EventHandler<MyGenericEventArgs<MyObject1>> OnMyEventTaken;
[EventSubscription(@"MyTopic", ThreadOption.Publisher)] // <- subscription for child views
public void OnOnMyEventTaken(object sender, MyGenericEventArgs<MyObject2> eventArgs)

View2 (child for View1, View1 is parent for View2)
[EventPublication(@"MyTopic", PublicationScope.WorkItem)] // <- publication for View1
public event EventHandler<MyGenericEventArgs<MyObject2>> OnMyEventTaken;

When OnMyEventTaken fires exception is rised that:
"Object of type 'MyGenericEventArgs`1[MyObject2]' cannot be converted to type 'MyGenericEventArgs`1[MyObject1]'."

Does anyone has any idea?
Oct 5, 2007 at 6:52 PM
Hi

This errors occurs because you have the same event topic name for different events.
For example, this event publication:

[EventPublication(@"MyTopic", PublicationScope.WorkItem)] 
public event EventHandler<MyGenericEventArgs<MyObject1>> OnMyEventTaken;
only can accept this event subscription:

EventSubscription(@"MyTopic", ThreadOption.Publisher)]
public void OnMyEventTaken(object sender, MyGenericEventArgs<MyObject1> eventArgs)
If you add an event subscription with a different signature that the event publication, you will get a runtime error when you raise the event.
I suggest you to use a different event topic name for each different event.
Example:

// Event1
[EventPublication(@"MyTopic1", PublicationScope.WorkItem)] 
public event EventHandler<MyGenericEventArgs<MyObject1>> OnMyEventTaken;
EventSubscription(@"MyTopic1", ThreadOption.Publisher)]
public void OnMyEventTaken(object sender, MyGenericEventArgs<MyObject1> eventArgs)
 
// Event2
[EventPublication(@"MyTopic2", PublicationScope.WorkItem)] 
public event EventHandler<MyGenericEventArgs<MyObject2>> OnMyEventTaken;
EventSubscription(@"MyTopic2", ThreadOption.Publisher)]
public void OnMyEventTaken(object sender, MyGenericEventArgs<MyObject2> eventArgs)
 
// …
 
//EventX
[EventPublication(@"MyTopicX", PublicationScope.WorkItem)] 
public event EventHandler<MyGenericEventArgs<MyObjectX>> OnMyEventTaken;
EventSubscription(@"MyTopicX", ThreadOption.Publisher)]
public void OnMyEventTaken(object sender, MyGenericEventArgs<MyObjectX> eventArgs)
Please let me know if this helps.

Mariano Converti
http://staff.southworks.net/blogs/mconverti/