【发布时间】:2012-10-23 16:41:44
【问题描述】:
我想使用反射订阅 EventAggregator 事件,因为我试图在运行时动态连接 Prism 模块之间的事件订阅。 (我正在使用 Silverlight 5、Prism 和 MEF)。
我想要实现的是在我的一个模块中调用_eventAggregator.GetEvent<MyType>().Subscribe(MyAction),但我坚持调用_eventAggregator.GetEvent<MyType>()。我怎样才能从那里拨打Subscribe(MyAction)?
假设我的事件类是public class TestEvent : CompositePresentationEvent<string> { }。我在编译时不知道这一点,但我在运行时知道 Type。
这是我目前得到的:
Type myType = assembly.GetType(typeName); //get the type from string
MethodInfo method = typeof(IEventAggregator).GetMethod("GetEvent");
MethodInfo generic = method.MakeGenericMethod(myType);//get the EventAggregator.GetEvent<myType>() method
generic.Invoke(_eventAggregator, null);//invoke _eventAggregator.GetEvent<myType>();
我真的很感激一个指向正确方向的指针。
【问题讨论】:
标签: c# silverlight reflection prism eventaggregator