0
我無法弄清楚這一點。我想傳入一個GenreID並獲得相應類型的對象列表,即如果我傳入MyGenre.RockAndRoll,我想要一個RockAndRoll對象列表。需要動態創建實現相同接口的對象(使用Activator.CreateInstance)
公共枚舉MyGenre 替代= 0 rockAndRoll = 1個 國家= 2 結束枚舉
Public Interface IGenre
Inherits IDisposable
Property title As String
Property artist As String
Property duration As Decimal
Property rating As Integer
Sub Listen()
End Interface
Public Class RockAndRoll
Implements IGenre
Public Property artist As String Implements IGenre.artist
Public Property duration As Decimal Implements IGenre.duration
Public Property rating As Integer Implements IGenre.rating
Public Property title As String Implements IGenre.title
Public Sub Listen() Implements IGenre.Listen
'...
End Sub
#Region "IDisposable Support"
'...
#End Region
End Class
Public Class Country
Implements IGenre
Public Property artist As String Implements IGenre.artist
Public Property duration As Decimal Implements IGenre.duration
Public Property rating As Integer Implements IGenre.rating
Public Property title As String Implements IGenre.title
Public Sub Listen() Implements IGenre.Listen
'...
End Sub
#Region "IDisposable Support"
'...
#End Region
End Class
Public Function GetAlternativeList() As IEnumerable(Of IGenre)
Return GetIGenreList(MyGenre.alternative)
End Function
Public Function GetrockAndRollList() As IEnumerable(Of IGenre)
Return GetIGenreList(MyGenre.alternative)
End Function
Public Function GetIGenreList(p_MyGenre As MyGenre) As IEnumerable(Of IGenre)
Using db As New OracleDataContext
Return (From s In db.SongList
Where s.Genre= MyGenre _
Select CType(Activator.CreateInstance(p_MyGenre.GetGenreType,
New With {.title = s.title, .artist = p1.artist, etc etc etc}), p_MyGenre.GetGenreTyp)).ToList
End Using
End Function
<Extension()> _
Private Function GetGenreType(p_formatID As MyGenre) As Type
Select Case p_formatID
Case MyGenre.rockAndRoll
Return GetType(RockAndRoll)
Case MyGenre.country
Return GetType(PlayawayView)
Case Else
Return Nothing
End Select
End Function
#
EDIT
#我想添加爲了清楚另外的實例。第一個令人困惑。讓我們從格式的角度來看這個。
我想返回一個產品列表(DVD,CD,藍光等),每種格式都有它自己的內部類(用於藝術品大小,處理,合法性原因等)。
Class CD -> Implments IProduct
Class DVD -> Implements IProduct
Class BLURAY -> Implements IProduct
i.e. dim MyList = (from p in MyProductTable select p).tolist
returns 4 records.
(0)
title: Motown Unmixed
artist: Various
duration: 36:25
format: CD
upc: 024543246157
(1)
title: Classical Bytes - Bach
artist: Various
duration: 54:32
format: CD
upc: 709387901743
(2)
title: Star Wars: Episode VI - Return of the Jedi
artist: null
duration: 136.00
format: DVD
upc: 883928446172
(3)
title: Perfect Stranger
artist: null
duration: 95.36
format: BLU
upc: 043215190627
我想要的是一個列表IPRODUCT。這可以通過點擊數據庫3次(對於每種格式類型)來完成。並將結果附加到IPRODUCT列表中。
dim MyProductList as new List(of IProduct)
MyProductList.addrange((from p in MyProductTable where p.format = "CD" select new CD with {.title = p.title, etc etc etc}).tolist)
MyProductList.addrange((from p in MyProductTable where p.format = "DVD" select new DVD with {.title = p.title, etc etc etc}).tolist)
MyProductList.addrange((from p in MyProductTable where p.format = "BLU" select new BLURAY with {.title = p.title, etc etc etc}).tolist)
我想將上面的3個查詢合併爲一個查詢。該查詢將返回CD,DVD和BLURAY對象的列表。
您不需要'Interface'來強制執行屬性。如果派生類需要它們,它們只需要在基類上標記爲Public。 – OneFineDay
我同意100%。我的實際代碼與音樂無關。這是一個示範我扔在一起,以便其他人可以更好地瞭解我想要做什麼。 – user1932634