【发布时间】:2018-03-28 06:18:22
【问题描述】:
我遇到的问题在以下代码中。该函数适用于 ControlCaption 实体类,但我必须为我需要从中读取的每个其他实体类复制并粘贴它,然后将 ControlCaption 更改为新的类名(我讨厌在不需要时重复代码)。因为还有一个更新/插入值的功能,我曾尝试将公共对象设置为等于该类(即 Public SomeClass as Object = New ControlCaption),但 IDE 抱怨它。
代码如下:
Public Function SelectList(Optional ByVal SQLString As String = "SELECT * FROM " & DatabaseName) As List(Of ControlCaption)
Dim strConnectionString As String = ConnectionString() '--Creates the connection string
Dim intLineNumber As Integer = 0
Dim InfoList As New List(Of ControlCaption) '--List of classes being returned
Try
If DatabaseType = SQLServer Then '--User indicated previously that a SQL Server was being used
Using myConnection As New SqlClient.SqlConnection(strConnectionString)
myConnection.Open()
Using myCommand As New SqlClient.SqlCommand(SQLString, myConnection)
Using myReader = myCommand.ExecuteReader
Do While myReader.Read
InfoList.Add(New ControlCaption()) '--Add a new element to the list
'--The next couple of lines need System.Reflection to be imported to work
'--The following for statement goes through each property in a class and assigns
'--the database value with the same name to the property. (Required to use entity classes)
Dim TheObject As New ControlCaption
Dim TheType As Type = TheObject.GetType()
Dim Properties() As PropertyInfo = TheType.GetProperties()
For Each Prop As PropertyInfo In properties
Try
If UCase(Prop.Name) <> "ITEM" Then
If TypeOf (myReader.Item(Prop.Name)) Is DateTime Then
'--Convert value to date
InfoList(InfoList.Count - 1).Item(Prop.Name) = CDate(IIf((myReader.Item(Prop.Name).ToString & String.Empty) = vbNullString, "1/1/1900", myReader.Item(Prop.Name).ToString))
Else
InfoList(InfoList.Count - 1).Item(Prop.Name) = myReader.Item(Prop.Name).ToString & String.Empty
End If
End If
Catch ex As Exception
End Try
Next
Loop
End Using
End Using
If myConnection.State <> ConnectionState.Closed Then
myConnection.Close()
End If
Return InfoList
End Using
ElseIf DatabaseType = AccessDatabase Then
Using myConnection As New OleDb.OleDbConnection(strConnectionString)
myConnection.Open()
Using myCommand As New OleDb.OleDbCommand(SQLString, myConnection)
Using myReader = myCommand.ExecuteReader
Do While myReader.Read
InfoList.Add(New ControlCaption())
Dim TheObject As New ControlCaption
Dim TheType As Type = TheObject.GetType()
Dim Properties() As PropertyInfo = TheType.GetProperties()
For Each Prop As PropertyInfo In properties
Try
If UCase(Prop.Name) <> "ITEM" Then
If TypeOf (myReader.Item(Prop.Name)) Is DateTime Then
InfoList(InfoList.Count - 1).Item(Prop.Name) = CDate(IIf((myReader.Item(Prop.Name).ToString & String.Empty) = vbNullString, "1/1/1900", myReader.Item("DateModified").ToString))
Else
InfoList(InfoList.Count - 1).Item(Prop.Name) = myReader.Item(Prop.Name).ToString & String.Empty
End If
End If
Catch ex As Exception
End Try
Next
Loop
End Using
End Using
If myConnection.State <> ConnectionState.Closed Then
myConnection.Close()
End If
Return InfoList
End Using
Else
Return Nothing
End If
Catch ex As Exception
Return Nothing
End Try
End Function
我觉得必须有办法让这个函数接受任何类,但我还没有想出办法。
有人对如何进行这项工作有任何建议吗?任何改进建议也会有所帮助。
谢谢。
对于任何感兴趣的人,这里是生成的最终代码:
Public Function SelectList(Of EntityClass As New)(Optional ByVal SQLString As String = "SELECT * FROM " & DatabaseName) As List(Of EntityClass)
Try
Dim Entities As New List(Of EntityClass)()
Using Connection As New SqlClient.SqlConnection(ConnectionString)
Using Command As New SqlClient.SqlCommand(SQLString, Connection)
Connection.Open()
Using Reader = Command.ExecuteReader()
Dim Properties = GetType(EntityClass).GetProperties()
Do While Reader.Read
Dim Entity = CreateEntity(Of EntityClass)(Reader, Properties)
Entities.Add(Entity)
Loop
End Using
End Using
End Using
Return Entities
Catch ex As Exception
MsgBox(Err.Description)
Return Nothing
End Try
End Function
Private Function CreateEntity(Of PassedEntity As New)(ByVal reader As DbDataReader, ByVal properties As PropertyInfo()) As PassedEntity
Dim Entity As New PassedEntity()
For Each _Property As PropertyInfo In properties
If _Property.Name.ToUpper() = "ITEM" Then Continue For
Dim value = reader.Item(_Property.Name)
_Property.SetValue(Entity, value, Nothing)
Next
End Function
【问题讨论】:
-
使其通用
-
你确定你的方法中带有默认值的可选参数被编译了吗?我担心默认值应该是常量值
-
类在 VB.NET 中对我来说相当新。感谢您的帮助。