LzmTW.uSystem.uReflection+DatasFunction

来源:互联网 发布:影楼修图软件 编辑:程序博客网 时间:2024/04/29 09:14

Author:水如烟  

Imports LzmTW.uSystem.uReflection.MemberInfoFunction

Namespace LzmTW.uSystem.uReflection

    
Public Class DatasFunction

        
Private Sub New()
        
End Sub

        
Public Shared Function CreateTableFromType(ByVal t As Type) As DataTable
            
Dim tmpTable As New DataTable

            
If TypeHasFields(t) Then
                
For Each f As Reflection.FieldInfo In t.GetFields
                    tmpTable.Columns.Add(f.Name, f.FieldType)
                
Next
            
Else
                
For Each p As Reflection.PropertyInfo In t.GetProperties
                    
If p.CanRead Then tmpTable.Columns.Add(p.Name, p.PropertyType)
                
Next
            
End If

            
Return tmpTable
        
End Function

        
Public Shared Function ItemToDataRow(Of T)(ByVal item As T, ByVal table As DataTable) As DataRow
            
Dim tmpRow As DataRow = table.NewRow

            
Dim mName As String
            
Dim mtype As Type = GetType(T)

            
For Each c As DataColumn In table.Columns
                mName 
= c.ColumnName
                
Dim obj As Object

                
If TypeHasFields(mtype) Then
                    obj 
= GetFieldResult(item, mName)
                
Else
                    obj 
= GetPropertyResult(item, mName, Nothing)
                
End If

                
If obj Is Nothing Then obj = DBNull.Value

                tmpRow(mName) 
= obj
            
Next

            
Return tmpRow
        
End Function

        
Public Shared Sub ItemAppendToTable(Of T)(ByVal item As T, ByVal table As DataTable)
            table.Rows.Add(ItemToDataRow(
Of T)(item, table))
        
End Sub

        
Public Shared Sub ItemAppendToTable(Of T)(ByVal items() As T, ByVal table As DataTable)
            
For Each item As T In items
                ItemAppendToTable(
Of T)(item, table)
            
Next
        
End Sub

        
Public Shared Function ItemsToTable(Of T)(ByVal items() As T) As DataTable
            
Dim mTable As DataTable = CreateTableFromType(GetType(T))

            
If items Is Nothing Then Return mTable

            ItemAppendToTable(
Of T)(items, mTable)

            
Return mTable
        
End Function

        
''' <summary>
        ''' 要求T有New()构造函数
        ''' </summary>
        Public Shared Function DataRowToItem(Of T)(ByVal row As DataRow) As T
            
Dim mType As Type = GetType(T)
            
Dim mResult As T = CType(System.Activator.CreateInstance(mType), T)

            
Dim mName As String

            
If TypeHasFields(mType) Then
                
For Each c As DataColumn In row.Table.Columns
                    mName 
= c.ColumnName
                    SetFieldValue(mResult, mName, row(mName))
                
Next
            
Else
                
For Each c As DataColumn In row.Table.Columns
                    mName 
= c.ColumnName
                    SetPropertyValue(mResult, mName, row(mName), 
Nothing)
                
Next
            
End If

            
Return mResult
        
End Function

        
Public Shared Function TableToItems(Of T)(ByVal table As DataTable) As T()
            
Dim mItems(table.Rows.Count - 1As T
            
For i As Integer = 0 To table.Rows.Count - 1
                mItems(i) 
= DataRowToItem(Of T)(table.Rows(i))
            
Next
            
Return mItems
        
End Function
    
End Class

End Namespace

原创粉丝点击