我有一本字典如下:
var composEvents = new Dictionary<Type, Delegate>
{
{
typeof (GetWorkflowAnalysisDealLevelViewDataCompletedEvent),
new Action<bool>(GetWorkflowAnalysisDealLevelViewDataCompleted)
},{
typeof (NoDataReturnedEvent),
new Action<NoDataReturnedParameters>(NoDataReturned)
}
};
然后我订阅不同的事件,将动作作为回调传递,但是每个动作的类型对于每个事件都是不同的,即:
Action<bool>
Action<NoDataReturnedParameters>
如何动态生成以下代码而不显式转换操作类型?
foreach (var cEvent in composEvents)
{
var method = typeof(IEventAggregator).GetMethod("GetEvent", BindingFlags.Public | BindingFlags.Instance);
var generic = method.MakeGenericMethod(cEvent.Key);
dynamic evt = generic.Invoke(_eventAggregator, null);
var cancelationToken = evt.Subscribe((Action<bool>)cEvent.Value);
_compositeEvents.Add(evt, cancelationToken);
}