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参数是无效的
关心! 。