我想使用Reflection來訂閱EventAggregator事件,因爲我試圖在運行時動態連接Prism模塊之間的事件訂閱。 (我使用的是Silverlight 5,Prism和MEF)。使用Reflection訂閱Prism EventAggregator事件
我想實現的是在我的一個模塊中調用_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>();
我真的很感激,在正確的方向指針。
var myEvent = generic.Invoke(eventAggregator, null) as CompositePresentationEvent<string>;
if (myEvent != null)
myEvent.Subscribe(MyAction);
假設你知道載荷類型:
是的,謝謝!我錯過了'作爲CompositePresentationEvent'部分。 –
Phasma