如何实现“句柄”通过Codedom
我一直在尝试弄清楚如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Handles只是vb.net为您提供的方便而提供的一个语法糖。
在幕后它被转换为:
所以我认为这是不可能的。您应该尝试使用 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:
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?