Author:水如烟
Imports
LzmTW.uSystem
Imports System.ComponentModel
Namespace LzmTW
Public Class ConfigManger( Of T)
Private gFileName As String = AppDomain.CurrentDomain.BaseDirectory & " {0}.{1}s.dat "
Private gCollection As New ConfigInformationCollection( Of T)
< Description( " 文件 " ), Category( " 配置 " ), Editor( GetType (Windows.Forms.Design.FileNameEditor), GetType (Drawing.Design.UITypeEditor)) > _
Public Property FileName() As String
Get
Return gFileName
End Get
Set ( ByVal value As String )
gFileName = value
End Set
End Property
< Description( " 项目集合 " ), Category( " 项目 " ), Editor( GetType (Design.CollectionEditor), GetType (Drawing.Design.UITypeEditor)) > _
Public ReadOnly Property Collection() As Collections.ObjectModel.Collection( Of T)
Get
Return gCollection
End Get
End Property
< Description( " 个数 " ), Category( " 项目 " ) > _
Public ReadOnly Property Count() As Integer
Get
Return Me .gCollection.Count
End Get
End Property
Sub New ()
gFileName = String .Format(gFileName, System.Reflection.Assembly.GetEntryAssembly.ManifestModule.Name, GetType (T).Name)
End Sub
Public Sub Add( ByVal item As T)
Me .gCollection.Add(item)
End Sub
Public Sub AddRange( ByVal items As T())
For Each item As T In items
Me .Add(item)
Next
End Sub
Public Sub Remove( ByVal item As T)
If Me .gCollection.Contains(item) Then
Me .gCollection.Remove(item)
End If
End Sub
Public Sub Clear()
Me .gCollection.Clear()
End Sub
Default Public ReadOnly Property Item( ByVal index As Integer ) As T
Get
If index > - 1 Then
If index < Me .Count Then
Return Me .gCollection.Item(index)
End If
End If
Return Nothing
End Get
End Property
Public Function GetEnumerator() As Collections.Generic.IEnumerator( Of T)
Return Me .gCollection.GetEnumerator
End Function
Public Function Find( ByVal memberName As String , ByVal value As Object ) As T
If Not uReflection.MemberInfoFunction.TypeHasMember( GetType (T), memberName) Then
Return Nothing ' Throw New Exception(String.Format("无此成员:{0}", memberName))
End If
Dim mResult As T = Nothing
Dim mIsField As Boolean = uReflection.MemberInfoFunction.TypeHasFields( GetType (T))
With Me .gCollection.GetEnumerator
While .MoveNext
If mIsField Then
Dim tmpValue As Object = uReflection.MemberInfoFunction.GetFieldResult(.Current, memberName)
If tmpValue IsNot Nothing AndAlso tmpValue.Equals(value) Then
mResult = .Current
Exit While
End If
Else
Dim tmpValue As Object = uReflection.MemberInfoFunction.GetPropertyResult(.Current, memberName, Nothing )
If tmpValue IsNot Nothing AndAlso tmpValue.Equals(value) Then
mResult = .Current
Exit While
End If
End If
End While
End With
Return mResult
End Function
Public Function Finds( ByVal memberName As String , ByVal value As Object ) As T()
If Not uReflection.MemberInfoFunction.TypeHasMember( GetType (T), memberName) Then
Return Nothing
End If
Dim mResult( - 1 ) As T
Dim mIsField As Boolean = uReflection.MemberInfoFunction.TypeHasFields( GetType (T))
With Me .gCollection.GetEnumerator
While .MoveNext
If mIsField Then
Dim tmpValue As Object = uReflection.MemberInfoFunction.GetFieldResult(.Current, memberName)
If tmpValue IsNot Nothing AndAlso tmpValue.Equals(value) Then
uCollection.CommonFunction.Append( Of T)(mResult, .Current)
End If
Else
Dim tmpValue As Object = uReflection.MemberInfoFunction.GetPropertyResult(.Current, memberName, Nothing )
If tmpValue IsNot Nothing AndAlso tmpValue.Equals(value) Then
uCollection.CommonFunction.Append( Of T)(mResult, .Current)
End If
End If
End While
End With
Return mResult
End Function
Public Sub ForEach( ByVal action As Action( Of T))
With Me .GetEnumerator
While .MoveNext
action.Invoke(.Current)
End While
End With
End Sub
Public Sub Read( ByVal file As String )
gFileName = file
Read()
End Sub
Public Sub Save( ByVal file As String )
gFileName = file
Save()
End Sub
Public Sub Read()
ReadInternal()
End Sub
Public Sub Save()
SaveInternal()
End Sub
Private Sub SaveInternal()
uSystem.uRuntime.uSerialization.SerializeHelper.Save( Of ConfigInformationCollection( Of T))(gFileName, uRuntime.uSerialization.FormatType.Binary, Me .gCollection)
End Sub
Private Sub ReadInternal()
Dim tmp As ConfigInformationCollection( Of T)
tmp = uSystem.uRuntime.uSerialization.SerializeHelper.Load( Of ConfigInformationCollection( Of T))(gFileName, uRuntime.uSerialization.FormatType.Binary)
With tmp.GetEnumerator
While .MoveNext
Me .gCollection.Add(.Current)
End While
End With
tmp.Clear()
tmp = Nothing
End Sub
Public Shared Function CreateInstance() As ConfigManger( Of T)
Return CType (System.Activator.CreateInstance( GetType (ConfigManger( Of T))), ConfigManger( Of T))
End Function
< Serializable() > _
Private Class ConfigInformationCollection( Of V)
Inherits Collections.ObjectModel.Collection( Of V)
End Class
End Class
End Namespace
Imports System.ComponentModel
Namespace LzmTW
Public Class ConfigManger( Of T)
Private gFileName As String = AppDomain.CurrentDomain.BaseDirectory & " {0}.{1}s.dat "
Private gCollection As New ConfigInformationCollection( Of T)
< Description( " 文件 " ), Category( " 配置 " ), Editor( GetType (Windows.Forms.Design.FileNameEditor), GetType (Drawing.Design.UITypeEditor)) > _
Public Property FileName() As String
Get
Return gFileName
End Get
Set ( ByVal value As String )
gFileName = value
End Set
End Property
< Description( " 项目集合 " ), Category( " 项目 " ), Editor( GetType (Design.CollectionEditor), GetType (Drawing.Design.UITypeEditor)) > _
Public ReadOnly Property Collection() As Collections.ObjectModel.Collection( Of T)
Get
Return gCollection
End Get
End Property
< Description( " 个数 " ), Category( " 项目 " ) > _
Public ReadOnly Property Count() As Integer
Get
Return Me .gCollection.Count
End Get
End Property
Sub New ()
gFileName = String .Format(gFileName, System.Reflection.Assembly.GetEntryAssembly.ManifestModule.Name, GetType (T).Name)
End Sub
Public Sub Add( ByVal item As T)
Me .gCollection.Add(item)
End Sub
Public Sub AddRange( ByVal items As T())
For Each item As T In items
Me .Add(item)
Next
End Sub
Public Sub Remove( ByVal item As T)
If Me .gCollection.Contains(item) Then
Me .gCollection.Remove(item)
End If
End Sub
Public Sub Clear()
Me .gCollection.Clear()
End Sub
Default Public ReadOnly Property Item( ByVal index As Integer ) As T
Get
If index > - 1 Then
If index < Me .Count Then
Return Me .gCollection.Item(index)
End If
End If
Return Nothing
End Get
End Property
Public Function GetEnumerator() As Collections.Generic.IEnumerator( Of T)
Return Me .gCollection.GetEnumerator
End Function
Public Function Find( ByVal memberName As String , ByVal value As Object ) As T
If Not uReflection.MemberInfoFunction.TypeHasMember( GetType (T), memberName) Then
Return Nothing ' Throw New Exception(String.Format("无此成员:{0}", memberName))
End If
Dim mResult As T = Nothing
Dim mIsField As Boolean = uReflection.MemberInfoFunction.TypeHasFields( GetType (T))
With Me .gCollection.GetEnumerator
While .MoveNext
If mIsField Then
Dim tmpValue As Object = uReflection.MemberInfoFunction.GetFieldResult(.Current, memberName)
If tmpValue IsNot Nothing AndAlso tmpValue.Equals(value) Then
mResult = .Current
Exit While
End If
Else
Dim tmpValue As Object = uReflection.MemberInfoFunction.GetPropertyResult(.Current, memberName, Nothing )
If tmpValue IsNot Nothing AndAlso tmpValue.Equals(value) Then
mResult = .Current
Exit While
End If
End If
End While
End With
Return mResult
End Function
Public Function Finds( ByVal memberName As String , ByVal value As Object ) As T()
If Not uReflection.MemberInfoFunction.TypeHasMember( GetType (T), memberName) Then
Return Nothing
End If
Dim mResult( - 1 ) As T
Dim mIsField As Boolean = uReflection.MemberInfoFunction.TypeHasFields( GetType (T))
With Me .gCollection.GetEnumerator
While .MoveNext
If mIsField Then
Dim tmpValue As Object = uReflection.MemberInfoFunction.GetFieldResult(.Current, memberName)
If tmpValue IsNot Nothing AndAlso tmpValue.Equals(value) Then
uCollection.CommonFunction.Append( Of T)(mResult, .Current)
End If
Else
Dim tmpValue As Object = uReflection.MemberInfoFunction.GetPropertyResult(.Current, memberName, Nothing )
If tmpValue IsNot Nothing AndAlso tmpValue.Equals(value) Then
uCollection.CommonFunction.Append( Of T)(mResult, .Current)
End If
End If
End While
End With
Return mResult
End Function
Public Sub ForEach( ByVal action As Action( Of T))
With Me .GetEnumerator
While .MoveNext
action.Invoke(.Current)
End While
End With
End Sub
Public Sub Read( ByVal file As String )
gFileName = file
Read()
End Sub
Public Sub Save( ByVal file As String )
gFileName = file
Save()
End Sub
Public Sub Read()
ReadInternal()
End Sub
Public Sub Save()
SaveInternal()
End Sub
Private Sub SaveInternal()
uSystem.uRuntime.uSerialization.SerializeHelper.Save( Of ConfigInformationCollection( Of T))(gFileName, uRuntime.uSerialization.FormatType.Binary, Me .gCollection)
End Sub
Private Sub ReadInternal()
Dim tmp As ConfigInformationCollection( Of T)
tmp = uSystem.uRuntime.uSerialization.SerializeHelper.Load( Of ConfigInformationCollection( Of T))(gFileName, uRuntime.uSerialization.FormatType.Binary)
With tmp.GetEnumerator
While .MoveNext
Me .gCollection.Add(.Current)
End While
End With
tmp.Clear()
tmp = Nothing
End Sub
Public Shared Function CreateInstance() As ConfigManger( Of T)
Return CType (System.Activator.CreateInstance( GetType (ConfigManger( Of T))), ConfigManger( Of T))
End Function
< Serializable() > _
Private Class ConfigInformationCollection( Of V)
Inherits Collections.ObjectModel.Collection( Of V)
End Class
End Class
End Namespace