反射、泛型、事件处理程序和委托问题
我正在与反射、泛型、事件处理程序和委托作斗争。我有一个派生自 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);
I’m struggling with reflection, generics, eventhandlers and delegates. I have a Type which is derived from AsyncCompletedEventArgs. I would like to create a generic EventHandler instance with this Type and also a delegate. Afterwards I want to add this eventhandler to an event.
Can anyone help my creating the eventhandler and delegate by using reflection? All help is appreciated.
// 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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只能为编译时已知的类型创建 lambda 表达式。
您正在尝试在运行时创建 lambda 表达式;为此,您需要编译一个表达式树。
You can only create a lambda expression for types known at compile time.
You're trying to create a lambda expression at runtime; to do that, you need to compile an expression tree.
查看 Delegate.CreateDelegate()
Look at Delegate.CreateDelegate()