0
我想用反射調用Prism EventAggregator,因爲事件有效載荷是在運行時確定的。通過反射調用方法時無效的參數
下面是使用EventAggregator正常方式:
[TestMethod]
public void WhenEventIsPublishedTheSubscriberReceivesIt()
{
var eventAggregator = new EventAggregator();
eventAggregator.GetEvent<PubSubEvent<string>>().Subscribe(Subscription);
eventAggregator.GetEvent<PubSubEvent<string>>().Publish(Constants.TestSymbol);
Assert.IsTrue(this.subscriptionReceived);
}
private void Subscription(string data)
{
this.subscriptionReceived = true;
}
我這樣做:
var commandHandlerLocator = this.container.Resolve<ICommandHandlerLocator>();
var t = command.GetType();
dynamic commandHandler = commandHandlerLocator.GetType()
.GetMethod("GetCommandHandler")
.MakeGenericMethod(command.GetType()).Invoke(commandHandlerLocator, null);
IEnumerable<IEvent> events = commandHandler.Execute(command);
foreach (var @event in events)
{
var typedEvent = typeof(PubSubEvent<>).MakeGenericType(@event.GetType());
dynamic pubSubEvent = this.eventAggregator.GetType()
.GetMethod("GetEvent")
.MakeGenericMethod(typedEvent).Invoke(this.eventAggregator, null);
pubSubEvent.Publish(@event);
}
看起來好像沒什麼問題。但是,當我執行最後一行「(pubSubEvent.Publish(@event);」我得到的例外,發佈方法被稱爲無效參數任何想法爲什麼@ event參數是無效的
關心! 。