Question

I have a list of CompositePresentationEvent like below:

var composEvents = new List<Type>
                               {
                                   typeof (GetWorkflowAnalysisDealLevelViewDataCompletedEvent),
                                   typeof (NoDataReturnedEvent),
                                   typeof (WorkflowDLVVisibilitiesChangedEvent),
                                   typeof (RetrieveWorkflowDLVDataForExport),
                                   typeof (LoadDLVTemplateEvent),
                                   typeof (SaveDLVTemplateEvent),
                                   typeof (PublishScreenCompositionEvent)
                               };

Previously I would create my events and subscribe to them using the below:

var evt1 = _eventAggregator.GetEvent<NoDataReturnedEvent>();
evt1.Subscribe(NoDataReturnedCallBack);

However I want to be able to do the above in a loop for each of the items in the list but when I try the following it won't work giving a "cannot resolve symbol cEvent":

foreach (var cEvent in composEvents)
{
   var tmpEvt = _eventAggregator.GetEvent<cEvent>();
   tmpEvt.Subscribe(NoDataReturned);
}

Can someone please show me an elegant way to achieve this?

Was it helpful?

Solution

I think this would help pretty much using Reflection . Use reflection also to invoke subscribe method :

  object myEvent = typeof(EventAggregator)
        .GetMethod("GetEvent")
        .MakeGenericMethod(cEvent)
        .Invoke(_eventAggregator,null);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top