gpt4 book ai didi

vb.net - 句柄与 AddHandler

转载 作者:行者123 更新时间:2023-12-05 09:00:36 25 4
gpt4 key购买 nike

动态附加/分离事件处理程序有优势吗?

手动分离处理程序是否有助于确保没有剩余对已处置对象的引用?

最佳答案

我很确定 Handles 子句只是语法糖,并将 AddHandler 语句插入到您的构造函数中。我使用这段代码进行了测试并禁用了应用程序框架,因此构造函数不会有额外的东西:


Public Class Form1

Public Sub New()
' This call is required by the Windows Form Designer. '
InitializeComponent()

' Add any initialization after the InitializeComponent() call. '
AddHandler Me.Load, AddressOf Form1_Load
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim breakpoint As Integer = 4
End Sub
End Class

IL 最终是这样的:

  IL_0000:  nop
IL_0001: ldarg.0
IL_0002: call instance void [System.Windows.Forms]System.Windows.Forms.Form::.ctor()
IL_0007: nop
IL_0008: ldarg.0
IL_0009: ldarg.0
IL_000a: dup
IL_000b: ldvirtftn instance void WindowsApplication1.Form1::Form1_Load(object,
class [mscorlib]System.EventArgs)
IL_0011: newobj instance void [mscorlib]System.EventHandler::.ctor(object,
native int)
IL_0016: call instance void [System.Windows.Forms]System.Windows.Forms.Form::add_Load(class [mscorlib]System.EventHandler)

'... lots of lines here '

IL_0047: ldarg.0
IL_0048: callvirt instance void WindowsApplication1.Form1::InitializeComponent()
IL_004d: nop
IL_004e: ldarg.0
IL_004f: ldarg.0
IL_0050: dup
IL_0051: ldvirtftn instance void WindowsApplication1.Form1::Form1_Load(object,
class [mscorlib]System.EventArgs)
IL_0057: newobj instance void [mscorlib]System.EventHandler::.ctor(object,
native int)
IL_005c: callvirt instance void [System.Windows.Forms]System.Windows.Forms.Form::add_Load(class [mscorlib]System.EventHandler)
IL_0061: nop
IL_0062: nop
IL_0063: ret
} // end of method Form1::.ctor

注意 IL_000b 和 IL_0051 周围的两个相同代码块。我认为这只是语法糖。

关于vb.net - 句柄与 AddHandler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com