LzmTW.uSystem.uReflection + GetSameTypeCollectionFromType

Author:水如烟  

Namespace  LzmTW.uSystem.uReflection

    
Public   Class  GetSameTypeCollectionFromType

        
Private  gMemberInfos  As   New  Dictionary( Of   String , Reflection.MemberInfo)
        
Private  gCollectList  As   String ()  =  { " IList " " ICollection " " IEnumerable " }

        
Public   ReadOnly   Property  Result()  As  Reflection.MemberInfo()
            
Get

                
Dim  tmp(gMemberInfos.Count  -   1 As  Reflection.MemberInfo
                gMemberInfos.Values.CopyTo(tmp, 
0 )

                
Return  tmp
            
End   Get
        
End Property

        
Public   Sub  Read( ByVal  type  As  Type,  Optional   ByVal  referBaseType  As   Boolean   =   False )

            
Me .GetFromFields(type, referBaseType)
            
Me .GetFromProperties(type, referBaseType)

        
End Sub

        
Public   Sub  Read( ByVal  item  As   Object Optional   ByVal  referBaseType  As   Boolean   =   False )
            
Dim  type  As  Type  =  item.GetType

            
Me .GetFromFields(type, referBaseType)
            
Me .GetFromProperties(type, referBaseType)

        
End Sub

        
Public   Sub  Clear()
            gMemberInfos.Clear()
        
End Sub

        
Private   Sub  Add( ByVal  propertyInfo  As  Reflection.PropertyInfo)
            
Dim  tmp  As   String   =   String .Concat(propertyInfo.PropertyType.FullName,  " + " , propertyInfo.Name)
            
If   Not  gMemberInfos.ContainsKey(tmp)  Then
                gMemberInfos.Add(tmp, propertyInfo)
            
End   If

        
End Sub

        
Private   Sub  Add( ByVal  fieldInfo  As  Reflection.FieldInfo)
            
Dim  tmp  As   String   =   String .Concat(fieldInfo.FieldType.FullName,  " + " , fieldInfo.Name)
            
If   Not  gMemberInfos.ContainsKey(fieldInfo.Name)  Then
                gMemberInfos.Add(fieldInfo.Name, fieldInfo)
            
End   If

        
End Sub

        
Private   Sub  AddByInterfaceType( ByVal  type  As  Type,  ByVal  propertyInfo  As  Reflection.PropertyInfo,  ByVal  PropertyTypeProperty  As  Reflection.PropertyInfo,  ByVal  referBaseType  As   Boolean )
            
If  referBaseType  Then
                
If  PropertyTypeProperty.PropertyType  Is  type.BaseType  Then
                    Add(propertyInfo)
                
End   If
            
Else
                
If  PropertyTypeProperty.PropertyType  Is  type  Then
                    Add(propertyInfo)
                
End   If
            
End   If
        
End Sub

        
Private   Sub  AddByArray( ByVal  type  As  Type,  ByVal  TypePropertyInfo  As  Reflection.PropertyInfo,  ByVal  TypePropertyInfoGetMemthodInfo  As  Reflection.MethodInfo,  ByVal  referBaseType  As   Boolean )
            
If  TypePropertyInfoGetMemthodInfo  IsNot   Nothing   Then
                
If  referBaseType  Then
                    
If  TypePropertyInfoGetMemthodInfo.ReturnType.Name.Equals(type.BaseType.Name  &   " [] " Then
                        Add(TypePropertyInfo)
                    
End   If
                
Else
                    
If  TypePropertyInfoGetMemthodInfo.ReturnType.Name.Equals(type.Name  &   " [] " Then
                        Add(TypePropertyInfo)
                    
End   If
                
End   If
            
End   If
        
End Sub

        
Private   Sub  AddByArray( ByVal  type  As  Type,  ByVal  TypeFieldInfo  As  Reflection.FieldInfo,  ByVal  referBaseType  As   Boolean )

            
If  referBaseType  Then
                
If  TypeFieldInfo.FieldType.Name.Equals(type.BaseType.Name  &   " [] " Then
                    Add(TypeFieldInfo)
                
End   If
            
Else
                
If  TypeFieldInfo.FieldType.Name.Equals(type.Name  &   " [] " Then
                    Add(TypeFieldInfo)
                
End   If
            
End   If

        
End Sub

        
Private   Sub  GetFromFields( ByVal  t  As  Type,  Optional   ByVal  referBaseType  As   Boolean   =   False )

            
For   Each  TypeFieldInfo  As  Reflection.FieldInfo  In  t.GetFields

                
If  TypeFieldInfo.FieldType.IsArray  Then

                    AddByArray(t, TypeFieldInfo, referBaseType)
                
End   If

            
Next

        
End Sub

        
Private   Sub  GetFromProperties( ByVal  t  As  Type,  Optional   ByVal  referBaseType  As   Boolean   =   False )


            
For   Each  TypePropertyInfo  As  Reflection.PropertyInfo  In  t.GetProperties

                
If  TypePropertyInfo.PropertyType.IsArray  Then

                    
Dim  TypePropertyInfoGetMemthodInfo  As  Reflection.MethodInfo  =  TypePropertyInfo.GetGetMethod( False )
                    AddByArray(t, TypePropertyInfo, TypePropertyInfoGetMemthodInfo, referBaseType)

                
Else
                    
For   Each  PropertyInterfaceType  As  Type  In  TypePropertyInfo.PropertyType.GetInterfaces

                        
If  Array.IndexOf(gCollectList, PropertyInterfaceType.Name)  <>   - 1   Then

                            
For   Each  PropertyTypeProperty  As  Reflection.PropertyInfo  In  TypePropertyInfo.PropertyType.GetProperties

                                AddByInterfaceType(t, TypePropertyInfo, PropertyTypeProperty, referBaseType)

                            
Next

                        
End   If

                    
Next
                
End   If

            
Next

        
End Sub

    
End Class

End Namespace

 

你可能感兴趣的:(辅助类代码)