0

我有以下接口:

Interface IViewModel
    ...
End Interface

Interface ISpecialViewModel
    Inherits IViewModel
    ...
End Interface

Interface IView
    WriteOnly Property MyViewModel As IViewModel
End Interface

以下是我的课程:

Class VerySpecialViewModel
    implements ISpecialViewModel
    ...
End Class

Class View
    Implements IView

    Public WriteOnly Property MyViewModel As VerySpecialViewModel Implements IView.MyViewModel 
        ...
    End Property
End Class

它告诉我“MyViewModel”无法实现“MyViewModel”,因为接口“IView”上没有匹配的属性。

4

1 回答 1

1
Public Interface ISomething
    WriteOnly Property Prop As IParent
End Interface

您的类实现不满足该接口声明。考虑以下情况:

还有另一个接口叫做 IChild2:

Public Interface IChild2
    Inherits IParent
    ...
End Interface

根据ISomething接口,您应该能够将实现的类的实例分配给IChild2Thing.Prop因为它继承了IParent。但是您不能,因为Thing.Prop属性是IChild类型并且IChild2不继承IChild

更新

那个解决方案怎么样:

Class ThingBase
    Implements ISomething

    Public WriteOnly Property Prop As IParent Implements ISomething.Prop
        Set(value As IParent)

        End Set
    End Property
End Class

Class Thing
    Inherits ThingBase

    Public Overloads WriteOnly Property Prop As IChild
        Set(value As IChild)
            MyBase.Prop = value
        End Set
    End Property
End Class

更新2

Interface IView(Of T As IViewModel)
    WriteOnly Property MyViewModel As T
End Interface

Class VerySpecialViewModel
    Implements ISpecialViewModel
End Class

Class View
    Implements IView(Of ISpecialViewModel)

    Public WriteOnly Property MyViewModel As ISpecialViewModel Implements IView(Of ISpecialViewModel).MyViewModel
        Set(value As ISpecialViewModel)

        End Set
    End Property
End Class

或者

Class View
    Implements IView(Of VerySpecialViewModel)

    Public WriteOnly Property MyViewModel As VerySpecialViewModel Implements IView(Of VerySpecialViewModel).MyViewModel
        Set(value As VerySpecialViewModel)

        End Set
    End Property
End Class
于 2013-03-13T07:16:56.413 回答