LzmTW + ConfigManger

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值