我有一个我试图实例化的单例类,它给出了异常“值不能为空”
我以我的主要形式声明了一个引用,例如:
Dim devices As DeviceUnderTestBindingList
然后在我的 form.load 中实例化:
devices = DeviceUnderTestBindingList.GetInstance
DeviceunderTestBindingList 类如下:
Imports System.ComponentModel
Public Class DeviceUnderTestBindingList
' DeviceUnderTest is one of my other regular classes...
Inherits System.ComponentModel.BindingList(Of DeviceUnderTest)
Private Shared SingleInstance As DeviceUnderTestBindingList
Private Shared InstanceLock As Object
Private Shared ListLock As Object
Private Sub New()
MyBase.New()
End Sub
Public Shared ReadOnly Property GetInstance As DeviceUnderTestBindingList
Get
' Ensures only one instance of this list is created.
If SingleInstance Is Nothing Then
SyncLock (InstanceLock)
If SingleInstance Is Nothing Then
SingleInstance = New DeviceUnderTestBindingList
End If
End SyncLock
End If
Return SingleInstance
End Get
End Property
End Class
我之前使用过相同的模式没有问题,现在突然导致异常,但是为什么呢?
请注意:这是一个 VB.NET Q!我已经阅读了很多处理类似问题的 C# Q,但对它们的理解还不够。