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 - 1) As 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
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 - 1) As 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
- LzmTW.uSystem.uReflection+DatasFunction
- LzmTW.uSystem.uReflection+MemberInfoFunction
- LzmTW.uSystem.uReflection + GetSameTypeCollectionFromType
- LzmTW.uSystem.uThreading+DelegateHandler
- LzmTW.uSystem.uThreading+CrossThread
- LzmTW.uSystem.uCollection.SinceLink
- LzmTW.uSystem.uWindows.AddIns
- LzmTW.uSystem.uData.uSql
- LzmTW.uSystem.uCollection + BaseTypeCompare
- LzmTW.uSystem.uCollection + SimpleFilter
- LzmTW.uSystem.uWindows.SafeNative+Constant
- LzmTW.uSystem.uWindows.SafeNative+SafeNativeMethods
- LzmTW.uSystem.uWindows.Win32API+DelegateHandler
- LzmTW.uSystem.uWindows.Win32API+MessageStructure
- LzmTW.uSystem.uWindows.Win32API+WindowsAction
- LzmTW.uSystem.uWindows.Win32API+MouseKeyboardHook
- LzmTW.uSystem.uWindows.Win32API+KeyStatusWather
- LzmTW.uSystem.uWindows.uForms + ToolPanel
- LzmTW.uSystem.uThreading+CrossThread
- 两种破解windows登陆密码
- 2天前开始学习游戏外挂制作,小有收获,嘿嘿
- SQL Reporting Services 困惑的解决 直接传递参数并用ReportViewer来呈现报表
- Reporting Services : 报表模型项目
- LzmTW.uSystem.uReflection+DatasFunction
- LzmTW.uSystem.uReflection+MemberInfoFunction
- 权威解释----如何选择羽毛球?(摘自《羽毛球竞赛规则问答》北京体育大学出版社
- Reporting Services : Web Service
- java异常处理陋习
- 如何判断一个整数是否为奇数
- 《论语》读书笔记 之一
- RDLC报表(一)(报表定义语言客户端处理)
- RDLC报表(二)