如何实现“句柄”通过Codedom

发布于 2024-09-14 06:22:51 字数 705 浏览 2 评论 0原文

我一直在尝试弄清楚如何使用 Codedom 将处理程序添加到方法中,但还没有走得太远。

我想通过 Codedom 重现的方法是:

Private Sub Startup() Handles btnStart.Click
    ''# Do work
End Sub

该方法很容易创建:

Dim StartupMethod As New CodeMemberMethod
StartupMethod.Name = "Startup"
StartupMethod.Attributes = MemberAttributes.Private

但我不知道如何添加 Handles btnStart.Click。我查看了 CodeAttachEventStatement< /a>,但是我不相信它可以对方法执行Handles

有谁知道如何实现这一目标?

编辑:下面的解决方案适用于 VB,但不适用于 C#,因为处理程序希望处理事件而不是方法。

I've been trying figure out how to add a handler to a method using Codedom, but am not getting very far.

The method I want to reproduce via Codedom is:

Private Sub Startup() Handles btnStart.Click
    ''# Do work
End Sub

The method is easy enough to create with:

Dim StartupMethod As New CodeMemberMethod
StartupMethod.Name = "Startup"
StartupMethod.Attributes = MemberAttributes.Private

But I can't figure out how to add the Handles btnStart.Click. I've looked at CodeAttachEventStatement, but this I don't believe it can do a Handles on a method.

Does anyone know how to achieve this?

EDIT: The solution below works for VB, but does not work for C# because the handler is looking to handle an event rather than a method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

瘫痪情歌 2024-09-21 06:22:51

Handles只是vb.net为您提供的方便而提供的一个语法糖。
在幕后它被转换为:

AddHandler btnStart.Click, AddressOf Startup

所以我认为这是不可能的。您应该尝试使用 CodeAttachEvent 语句

http://msdn .microsoft.com/en-us/library/system.codedom.codeattacheventstatement.aspx

另一种可能性是使用 CodeSnippetTypeMember(),如下所述

使用 CodeDom 的 VB.NET 部分方法的解决方法?

Handles is just a syntactic sugar vb.net offers you for your convenience.
Under the hood it is converted to:

AddHandler btnStart.Click, AddressOf Startup

So I think it won't be possible. You should try to use the CodeAttachEvent statement instead

http://msdn.microsoft.com/en-us/library/system.codedom.codeattacheventstatement.aspx

Another possibility is to use CodeSnippetTypeMember() as described here

Workaround for VB.NET partial method using CodeDom?

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