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# be开发者_运维技巧cause the handler is looking to handle an event rather than a method.
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?
精彩评论