反射、泛型、事件处理程序和委托问题

发布于 2024-12-06 12:34:29 字数 584 浏览 0 评论 0原文

我正在与反射、泛型、事件处理程序和委托作斗争。我有一个派生自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尘世孤行 2024-12-13 12:34:29

您只能为编译时已知的类型创建 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文