Prism - EventAggregator.GetEvent<>.Subscribe() - Using Generics & Contraints

爷,独闯天下 提交于 2019-12-20 03:35:23

问题


I am having an issue subscribing to events with the event Aggregator that comes as part of the prism framework.

If I use something such as

eventAggregator.GetEvent<string>().Subscribe(MyMethod)

then it all works ok, my method fires when an event has been published.

However, when moving to a more complex object other than I string I run into problems.

I have a bunch of classes that all derive from an Interface (IRequest) for example

I have my event class setup as follows

public class MyEvent<TRequest> : PubSubEvent<MyClass<TRequest>> where TRequest : IRequest {}

I have a generic class (MyClass) that uses an object of IRequest inside it - again, all seems to work ok at this point.

Now, lets say I publish an event of MyClass that uses the object Profile inside it:

eventAggregator.GetEvent<MyEvent<Profile>>().Publish(myProfileObject);

In my subscription I would like a method that can catch all events of MyEvent - Irrelevant of whether T is Profile or some other object that derives from IRequest - this is where I seem to be having problems.

In the following examples, the first works, but the second doesn't - I would ideally like to use something similar to the second.

eventAggregator.GetEvent<MyEvent<Profile>>().Subscribe(test1);
void test1 (MyClass<Profile> obj)
{
  //Some stuff here
}

eventAggregator.GetEvent<MyEvent<IRequest>>().Subscribe(test2);
void test2<T>(MyClass<T> obj) where T : IRequest
{
  //never gets called
}

My assumption is that because Profile derives from IRequest then it should work??? But it doesn't!!

Any help would be appreciated.

UPDATE
If I use the following, it does work, but would require me to create a separate subscription for each type of IRequest available - I'm looking to only have the one subscription.

eventAggregator.GetEvent<MyEvent<Profile>>().Subscribe(test2);
eventAggregator.GetEvent<MyEvent<User>>().Subscribe(test2);

回答1:


MyEvent<IRequest> and MyEvent<Profile> are not the same, so the EventAggregator doesn't see this as the same event. To allow for covariant types, you can fix it with the out modifier in C# 4. Something like this should work:

public interface IMyClass<out T> where T : IRequest { }

public class MyClass<T> : IMyClass<T> where T : IRequest { }

public class MyEvent<TValue> : PubSubEvent<IMyClass<TValue>> where TValue : IRequest { }

Then your event aggregator would look something like:

 _eventAggregator.GetEvent<MyEvent<IRequest>>().Subscribe(Received);

 private void Received(IMyClass<IRequest> obj)
 {

 }

_eventAggregator.GetEvent<MyEvent<IRequest>>().Publish(new MyClass<Profile>());

Does this help?



来源:https://stackoverflow.com/questions/33017900/prism-eventaggregator-getevent-subscribe-using-generics-contraints

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!