在泛型中创建约束时我犯了什么错误?

时间:2010-01-26 18:30:20

标签: vb.net generics constraints

在asp.net 2.0中,我使用泛型定义了几个“下拉列表”(例如眼睛颜色,头发颜色等)。这些领域都很典型; id,text等等都被定义为它们自己的类,它们必须实现我创建的一个名为ILookup的接口。但是,当我尝试返回List<>这个班的使用:

ddlEyeColor.DataSource = luMgt.GetLookUpItemList(Of EyeColor)()

调用BC层:

  Public Function GetLookUpItemList(Of t As {ILookup, New})() As List(Of t)
    Dim luMgt As New LookupMgt
    Return luMgt.GetLookUpItemList(Of t)()
End Function

调用DBC层,部分是....

    Public Function GetLookUpItemList(Of t As {ILookup, New})() As List(Of t)

    Dim lstGenericList As New List(Of t)
    'rest of code to populate the list here
    end function

错误消息声称EyeColor未实现ILookup。

错误21类型参数'EyeColor'不继承或实现约束类型'ILookup'。

但这是EyeColor类的开始......

Public Class EyeColor
Implements ILookup

这是完整的界面......

Public Interface ILookup

Property ID() As Int32
Property Text() As String
Property Description() As String
Property Status() As Status
Property OrderID() As Int32
ReadOnly Property LookUpType() As LookUpType

结束界面

在这里,在EyeColor类中,我正在实现接口

的特性
Public Overrides Property Description() As String Implements ILookup.Description
    Get
        Return MyBase.Description
    End Get
    Set(ByVal value As String)
        MyBase.Description = value
    End Set
End Property

Public Overrides Property ID() As Integer Implements ILookup.ID
    Get
        Return MyBase.ID
    End Get
    Set(ByVal value As Integer)
        MyBase.ID = value
    End Set
End Property

Public Overrides Property OrderID() As Integer Implements ILookup.OrderID
    Get
        Return MyBase.OrderID
    End Get
    Set(ByVal value As Integer)
        MyBase.OrderID = value
    End Set
End Property

Public Overrides Property Status() As Status Implements ILookup.Status
    Get
        Return MyBase.Status
    End Get
    Set(ByVal value As Status)
        MyBase.Status = value
    End Set
End Property

Public Property EyeColor() As String Implements ILookup.Text
    Get
        Return _eyeColor
    End Get
    Set(ByVal value As String)
        _eyeColor = value
    End Set
End Property

Public ReadOnly Property LookUpType() As LookUpType Implements ILookup.LookUpType
    Get
        Return BE.LookUpType.EyeColor
    End Get
End Property

2 个答案:

答案 0 :(得分:0)

我赞成杰伊的评论。他是完全正确的。简单地将Implements ILookup添加到类定义中实际上并不实现接口。

您需要创建接口所指示的实际方法。

答案 1 :(得分:0)

我认为您需要将方法签名更改为:

Public Function GetLookUpItemList(Of T As {ILookup, New})() As List(Of ILookup)