我遇到的問題是在下面的代碼。該函數適用於ControlCaption實體類,但我必須將其複製並粘貼到其他需要讀取的其他實體類,然後將ControlCaption更改爲新類名(我不需要重複代碼)。因爲還有一個更新/插入值的函數,我嘗試將公共對象設置爲與該類相同(即Public SomeClass爲Object = New ControlCaption),但IDE對此抱怨。VB.NET - 從函數返回動態類值
下面是代碼:
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
使其具有通用性 – djv
您確定編譯了方法中具有默認值的可選參數嗎?我擔心默認值應該是不變的值 – Fabio
VB.NET中的類對我來說是相當新的。謝謝你的幫助。 –