Public Interface ISomething
WriteOnly Property Prop As IParent
End Interface
您的类实现不满足该接口声明。考虑以下情况:
还有另一个接口叫做 IChild2:
Public Interface IChild2
Inherits IParent
...
End Interface
根据ISomething
接口,您应该能够将实现的类的实例分配给IChild2
,Thing.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