我正在努力反思,泛型,事件处理程序和代表。我有一个从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