作为整数
Public Class GetUniqueNumber
Private Shared id As Integer = 0
Private Shared lock As New Object
Public ReadOnly Property getID() As Integer
Get
Dim temp As Integer
SyncLock lock
temp = id
id += 1
End SyncLock
Return temp
End Get
End Property
End Class
作为字节数组
Public Class GetUniqueNumber
Private Shared id As Integer = 0
Private Shared lock As New Object
Public ReadOnly Property getID() As Byte()
Get
Dim temp As Byte()
SyncLock lock
temp = BitConverter.GetBytes(id)
id += 1
End SyncLock
Return temp
End Get
End Property
End Class
作为来自 int16 的字节数组
Public Class GetUniqueNumber
Private Shared id As Int16 = 0
Private Shared lock As New Object
Public ReadOnly Property getID() As Byte()
Get
Dim temp As Byte()
SyncLock lock
temp = BitConverter.GetBytes(id)
id += 1S
End SyncLock
Return temp
End Get
End Property
End Class
使用大整数
Public Class GetUniqueNumber
Private Shared id As BigInteger = 0
Private Shared lock As New Object
Public ReadOnly Property getID() As Byte()
Get
Dim temp As Byte()
SyncLock lock
temp = id.ToByteArray
id += 1
End SyncLock
If temp.Length <> 16 Then
Array.Resize(temp, 16)
End If
Return temp
End Get
End Property
End Class
或者
Public Class GetUniqueNumber
Private Shared idLS As Long = 0L
Private Shared idMS As Long = 0L
Private Shared lock As New Object
Public ReadOnly Property getID() As Byte()
Get
Dim tempLS() As Byte
Dim tempMS() As Byte
Dim rv(15) As Byte
SyncLock lock
tempLS = BitConverter.GetBytes(idLS)
tempMS = BitConverter.GetBytes(idMS)
If idLS = Long.MaxValue Then
idMS += 1L
idLS = 0L
Else
idLS += 1L
End If
Array.Reverse(tempLS)
Array.Reverse(tempMS)
Array.Copy(tempLS, 0, rv, 8, tempLS.Length)
Array.Copy(tempMS, 0, rv, 0, tempMS.Length)
End SyncLock
Return rv
End Get
End Property
End Class