需要动态创建实现相同接口的对象(使用Activator.CreateInstance)



我想不通。我想传入一个GenreID并获得相应流派的对象列表,即如果我传入MyGenre.RockAndRoll,我想要一个RockAndRoll对象列表。

公共枚举MyGenre备选方案=0摇滚乐=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
#

编辑

#

为了清楚起见,我想再加一个例子。第一个问题令人困惑。让我们从格式的角度来看。

我想返回一份产品列表(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次(针对每种格式类型)来完成。并将结果附加到IPRPRODUCTCT列表中。

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对象的列表。

我更简单的方法是放弃接口,用流派类型制作一个枚举,然后你只需要一个类,在那里你可以选择每个类的流派。

Dim query = db.Songlist.Where(Function(s) s.genre = genre).ToList

相关内容

  • 没有找到相关文章

最新更新