我正在努力反思,泛型,事件處理程序和代表。我有一個從AsyncCompletedEventArgs派生的類型。我想用這個類型創建一個通用的EventHandler實例,也是一個委託。之後,我想將此事件處理程序添加到事件中。反射,泛型,事件處理程序和代表問題
任何人都可以通過使用反射幫助我創建事件處理程序和委託嗎?所有的幫助表示讚賞。
// The type is derived from AsyncCompletedEventArgs
Type[] typeArgs = { soapServiceInfo.GetEntitiesCompletedEventArgsType };
var eventHandlerType = typeof(EventHandler<>);
var constructed = eventHandlerType.MakeGenericType(typeArgs);
// getEntitiesCompleted = (sender, arguments) => { }
// soapServiceInfo.GetEntitiesCompletedEventInfo.AddEventHandler(client, getEntitiesCompleted);
你想讓代表做什麼? – SLaks
你能提供AddEventHandler()的簽名嗎?它是(object,AsyncCompletedEventArgs)還是它(object,T)? –
簽名是(object,T)where T:AsyncCompletedEventArgs。 我一直在嘗試調用CreateDelegate,但是我從lambda函數獲取MethodInfo時遇到了問題。 GetEntitiesCompletedEventInfo是一個EventInfo對象。 – scipbe