If an interface defines a ReadOnly Property, how can an implementer provide the Setter to this property?

前端 未结 5 1088
北恋
北恋 2021-01-18 04:51

Is there a way for implementers of an interface where a ReadOnly property is defined to make it a complete Read/Write Property ?

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-18 05:22

    In the end, I ended up with a solution that matches my goal :

    • users that access via the Interface see at least a getter
    • users that access the implementation can Read and Write.

    I did this "shadowing" the implemented property like this :

    'users who access through interface see only the Read accessor
    Public ReadOnly Property PublicProperty_SomeInterface As String Implements SomeInterface.PublicProperty
        Get
            Return _myProperty
        End Get
    End Property
    
    
    'users who work with the implementation have Read/Write access
    Public Property PublicProperty_SomeInterface As String
        Get
            Return _myProperty
        End Get
        Set(ByVal value As String)
            _myProperty = value
        End Set
    End Property
    

    Then, depending on how you use it, you can do :

    Dim implementorAsInterface As SomeInterface = New InterfaceImplementor()
    logger.Log(implementor.PublicProperty) 'read access is always ok
    implementor.PublicProperty = "toto" 'compile error : readOnly access
    
    Dim implementor As InterfaceImplementor = New InterfaceImplementor()
    implementor.PublicProperty = "toto" 'write access
    

提交回复
热议问题