更新时间:2023-12-06 11:02:52
这里是 lambda 表达式让您陷入困境.不要深挖,只需使用 AddressOf 和私有方法,这样您就可以轻松使用 RemoveHandler 语句.
It is the lambda expression that is getting you into trouble here. Don't dig a deeper hole, just use AddressOf and a private method instead so you can trivially use the RemoveHandler statement.
如果您绝对必须记住,VB.NET 编译器会自动为事件生成一个后备存储字段,该字段与附加了事件"的事件同名.这使此代码工作:
If you absolutely have to then keep in mind that the VB.NET compiler auto-generates a backing store field for the event with the same name as the event with "Event" appended. Which makes this code work:
Dim Obj = New SimpleClass
AddHandler Obj.SimpleEvent, Sub()
MsgBox("Hi !")
End Sub
Dim fi = GetType(SimpleClass).GetField("SimpleEventEvent", BindingFlags.NonPublic Or BindingFlags.Instance)
fi.SetValue(Obj, Nothing)
Obj.SimpleMethod() '' No message box
我会重申你不应该这样做.
I'll reiterate that you should not do this.