如何遮蔽接口属性?

时间:2014-05-17 07:52:13

标签: vb.net inheritance interface

我有一个继承自非通用接口的通用接口。不知怎的,编译器不喜欢我是如何做到这一点的。我非常感谢帮助找出如何以编译器没有抱怨的方式完成这种继承。

Public Interface IMessage

    Property MessageType As UInt16

    Property Payload As Object

End Interface


Public Interface IMessage(Of T)
    Inherits IMessage

    Shadows Property Payload As T

End Interface


Public Class Message(Of T)
    Implements IMessage(Of T)    '  <-- This line is highlighted in VS.



    Public Property MessageType As UInt16 = 0 Implements IMessage(Of T).MessageType

    Public Property Payload As T = Nothing Implements IMessage(Of T).Payload

End Class
  

Class&#39; Message&#39;必须实施物业有效载荷作为对象&#39;对于   界面&#39; IMessage&#39;。实现属性必须匹配   &#39;只读&#39;或者&#39; WriteOnly&#39;说明符。

1 个答案:

答案 0 :(得分:4)

  Shadows Property Payload As T

通过使用 Shadows ,您告诉编译器此Payload属性故意与基本属性不匹配。因此,通用接口具有两个属性,一个As Object,另一个As T。因此,实现接口需要您实现它们:

Public Class Message(Of T)
    Implements IMessage(Of T)

    Public Property MessageType As UInt16 = 0 Implements IMessage(Of T).MessageType
    Public Property Payload As T = Nothing Implements IMessage(Of T).Payload
    Private Property Payload1 As Object Implements IMessage.Payload
        Get
            Return Payload
        End Get
        Set(value As Object)
            Payload = CType(value, T)
        End Set
    End Property
End Class

这不是你想要做的,我确定。因此,最好解决实际问题,非通用接口没有明显的用途。如果你有一个,那么该片段会告诉你如何处理它。