Вопрос

I would like to use

eventAggregator.GetEvent(typeof(SomeDomainEvent)).Subscribe();

instead of

eventAggregator.GetEvent<SomeDomainEvent>().Subscribe();

But GetEvent is not overloaded for that. How can I solve it?

Thank you.

Это было полезно?

Решение

You will need to use reflection.

// Equivalent to:
//
// SomeDomainEvent eventObject = eventAggregator.GetEvent<SomeDomainEvent>();
MethodInfo getEventMethod = typeof(IEventAggregator).GetMethod("GetEvent");
MethodInfo genericGetEventMethod = getEventMethod.MakeGenericMethod(typeof(SomeDomainEvent));
object eventObject = genericGetEventMethod.Invoke(eventAggregator, null);

// Now choose the version of "Subscribe" you want to call
// Equivalent to:
// 
// eventObject.Subscribe(MyEventHandler);
Type[] subscribeTypes = new Type[] { typeof(Action<SomeDomainEventArgs>) });
MethodInfo subscribeMethod = eventObject.GetType().GetMethod("Subscribe", subscribeTypes);
subscribeMethod.Invoke(eventObject, new object[] { new Action<SomeDomainEventArgs>(MyEventHandler) });
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top