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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值