Imports System.Collections.Generic
Imports System.Data.OleDb
Imports System.Reflection
Public Class ParametersBuilder(Of T)
Private allNames As String()
Private setNames As List(Of String)
Sub New()
Dim props() As PropertyInfo = GetType(T).GetProperties
allNames = (From p In props Select p.Name).ToArray
setNames = allNames.ToList
End Sub
Public Sub SetFields(ByVal ParamArray fields() As String)
Dim list As New List(Of String)
For Each field In fields
If allNames.Contains(field) Then
list.Add(field)
End If
Next
If list.Count > 0 Then
setNames = list
End If
End Sub
Public Function GetParameters(ByVal obj As T) As OleDbParameter()
Dim list As New List(Of OleDbParameter)
For Each name In setNames
Dim prop As PropertyInfo = GetType(T).GetProperty(name)
Dim p As New OleDbParameter
p.ParameterName = name
p.Value = prop.GetValue(obj, Nothing)
If p.Value IsNot Nothing Then
list.Add(p)
End If
Next
Return list.ToArray
End Function
Public Function GetFields(ByVal parms As OleDbParameter()) As String
Dim arr = From p In parms Select p.ParameterName
Return String.Join(",", arr)
End Function
Public Function GetVFields(ByVal parms As OleDbParameter()) As String
Dim arr = From p In parms Select "@" & p.ParameterName
Return String.Join(",", arr)
End Function
End Class
OleDbParameter 反射
最新推荐文章于 2021-04-08 00:36:15 发布