16,555
社区成员
发帖
与我相关
我的任务
分享
Namespace LzmTW.uSystem
Public Class Collection
Private Sub New()
End Sub
Public Shared Sub ForEach(Of T)(ByVal arrayObject As Object, ByVal action As Action(Of T))
If arrayObject Is Nothing Then Return
Dim type As Type = arrayObject.GetType
If type.IsArray Then
ForEach(Of T)(CType(arrayObject, Array), action)
ElseIf type Is GetType(List(Of T)) Then
ForEach(Of T)(CType(arrayObject, List(Of T)), action)
Else
action(Converter.ChangeTo(Of T)(arrayObject))
End If
End Sub
Public Shared Sub ForEach(Of T)(ByVal list As List(Of T), ByVal action As Action(Of T))
If list Is Nothing Then Return
ForEach(Of T)(list.GetEnumerator, action)
End Sub
Public Shared Sub ForEach(Of T)(ByVal array As IEnumerable(Of T), ByVal action As Action(Of T))
If array Is Nothing Then Return
ForEach(Of T)(array.GetEnumerator, action)
End Sub
Public Shared Sub ForEach(Of T)(ByVal array As IEnumerator(Of T), ByVal action As Action(Of T))
If array Is Nothing Then Return
With array
While .MoveNext
action(.Current)
End While
End With
End Sub
Public Shared Sub ForEach(Of T)(ByVal array As IEnumerable, ByVal action As Action(Of T))
If array Is Nothing Then Return
ForEach(Of T)(array.GetEnumerator, action)
End Sub
Public Shared Sub ForEach(Of T)(ByVal array As IEnumerator, ByVal action As Action(Of T))
If array Is Nothing Then Return
With array
While .MoveNext
action(Converter.ChangeTo(Of T)(.Current))
End While
End With
End Sub
Public Shared Function GetList(Of T)(ByVal arrayObject As Object) As List(Of T)
Dim helper As New GetObject(Of T)
ForEach(Of T)(arrayObject, AddressOf helper.Action)
Return helper.Result
End Function
Private Class GetObject(Of T)
Private List As New List(Of T)
Public Sub Action(ByVal value As T)
List.Add(value)
End Sub
Public ReadOnly Property Result() As List(Of T)
Get
Return List
End Get
End Property
End Class
End Class
End Namespace
Namespace LzmTW.uSystem
Public MustInherit Class IComClassBase(Of T)
Implements IDisposable
Private internalCom As T
Sub New(ByVal com As T)
Me.internalCom = com
End Sub
Public ReadOnly Property com() As T
Get
Return Me.internalCom
End Get
End Property
Public ReadOnly Property IsValid() As Boolean
Get
Return Not Me.internalCom Is Nothing
End Get
End Property
Protected Overridable Sub ReleaseOtherComsBeforeReleaseMainCom()
End Sub
Protected Overridable Sub DisposeObjectsBeforeReleaseMainCom()
End Sub
Public Function GetList(Of V)(ByVal arrayObject As Object) As List(Of V)
Return Collection.GetList(Of V)(arrayObject)
End Function
Public Sub ForEach(Of V)(ByVal arrayObject As Object, ByVal action As Action(Of V))
Collection.ForEach(Of V)(arrayObject, action)
End Sub
Public Function ChangeTo(Of V)(ByVal value As Object) As V
Return Converter.ChangeTo(Of V)(value)
End Function
Protected Sub ReleaseCom(ByVal com As Object)
If Not com Is Nothing AndAlso Marshal.IsComObject(com) Then
Marshal.ReleaseComObject(com)
com = Nothing
End If
End Sub
Private disposedValue As Boolean = False
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
Me.DisposeObjectsBeforeReleaseMainCom()
End If
Me.ReleaseOtherComsBeforeReleaseMainCom()
Me.ReleaseCom(Me.internalCom)
End If
Me.disposedValue = True
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
GC.Collect(2)
End Sub
End Class
End Namespace
Namespace LzmTW.uSystem
Public Class Converter
Private Sub New()
End Sub
Public Shared Function ChangeTo(Of T)(ByVal value As Object) As T
If value Is Nothing Then Return Nothing
Return CType(Microsoft.VisualBasic.CompilerServices.Conversions.ChangeType(value, GetType(T)), T)
End Function
End Class
End Namespace
Namespace LzmTW.uSystem
Public Class iniFile
Private gFile As String
Private gCurrentSection As String
Public Property File() As String
Get
Return gFile
End Get
Set(ByVal value As String)
gFile = value
End Set
End Property
Public Property CurrentSection() As String
Get
Return gCurrentSection
End Get
Set(ByVal value As String)
gCurrentSection = value
End Set
End Property
Public Function GetValue(ByVal key As String) As String
Return GetValue(key, Me.CurrentSection, Me.File)
End Function
Public Function SetValue(ByVal key As String, ByVal value As String) As Boolean
Return SetValue(key, value, Me.CurrentSection, Me.File)
End Function
Public Shared Function SetValue(ByVal key As String, ByVal value As String, ByVal section As String, ByVal file As String) As Boolean
Return WritePrivateProfileString(section, key, value, file)
End Function
Public Shared Function GetValue(ByVal key As String, ByVal section As String, ByVal file As String) As String
Dim mResult As New System.Text.StringBuilder(" ", 260)
GetPrivateProfileStringA(section, key, "", mResult, mResult.Capacity, file)
Return mResult.ToString
End Function
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" ( _
ByVal lpSectionName As String, _
ByVal lpKeyName As String, _
ByVal lpString As String, _
ByVal lpFileName As String _
) As Boolean
Private Declare Function GetPrivateProfileStringA Lib "kernel32.dll" ( _
ByVal lpSectionName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
ByVal nSize As Integer, _
ByVal lpFileName As String _
) As Integer
End Class
End Namespace
Namespace LzmTW.uSystem
Public Class SecurePassword
Private Password As Security.SecureString
Public ReadOnly Property SecureString() As Security.SecureString
Get
Return Password
End Get
End Property
Public Sub Reset(ByVal pass As String)
Me.Release()
If pass Is Nothing Then Return
pass = pass.Trim
If pass.Equals(String.Empty) Then Return
Password = New Security.SecureString
For Each c As Char In pass.ToCharArray
Password.AppendChar(c)
Next
Password.MakeReadOnly()
End Sub
Public Sub Release()
If Password Is Nothing Then Return
Password.Dispose()
Password = Nothing
End Sub
End Class
End Namespace