开发者

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

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

Imagine I define an interface to provide a ReadOnly Property (i.e., just a getter for a given value) :

Interface SomeInterface

    'the interface only say that implementers must provide a value for reading
    ReadOnly Property PublicProperty As String

End Interface

This means implementers must commit to providing a value. But I would like a given implementer to also allow setting that value. In my head, this would mean providing the Property's setter as part of the implementation, doing something like this :

Public Property PublicProperty As String Implements SomeInterface.PublicProperty
    Get
        Return _myProperty
    End Get
    Set(ByVal value As String)
        _myProperty = value
    End Set
End Property

but this will not compile as, for the VB compiler, the implementer no longer implements the interface (because it is no longer ReadOnly).

Conceptually, this should work, because, in the end, it just means Implement the getter from the Interface, and add a setter method. For "normal methods", this would be no problem.

Is there some way of doing it, without resorting to "interface hiding" or "home-made" SetProperty() method, and style having the Property behave like a Read/Write property in the implementations ?

Thanks !

--U开发者_如何学CPDATE-- (I have moved this question to a separate Question) My question is really : "why can't this be done in VB.NET", when the following is valid in C#.NET?" :

interface IPublicProperty
{
    string PublicProperty { get; }
}

with implementation :

public class Implementer:IPublicProperty
    {
        private string _publicProperty;

        public string PublicProperty
        {
            get
            {
                return _publicProperty;
            }
            set
            {
                _publicProperty = value;
            }
        }
    }


Now supported in Visual Studio 2015.

What's New for Visual Basic

Readonly Interface Properties

You can implement readonly interface properties using a readwrite property. The interface guarantees minimum functionality, and it does not stop an implementing class from allowing the property to be set.


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


There is nothing at a CLI level which prevents this type of implementation and as you've demonstrated it's already supported by C#. The VB.Net language just doesn't support this style of implementation.

Knowing why though is a bit tough since the decision is almost 10 years removed at this point. Very likely it was just an over site at the time interface implementation was designed.


You can't do it as the interface requires that you implement a ReadOnly Property.

Properties don't allow overloading so there is no way to implement a non-ReadOnly and also a ReadOnly.

I would suggest you either implement a Custom Setter or drop the ReadOnly from the Interface.

Without details of why you want to do this it is hard to suggest the best solution


In Visual Basic, when you implement a method or property from an interface, you can change its name and its visibility. You can leverage that capability to handle the case you are asking about. Prior to Visual Studio 2015, I often did this:

Interface:

Public Interface SomeInterface
    ReadOnly Property SomeProperty As String
End Interface

Implementing Class:

Public Class SomeClass
    Implements SomeInterface

    Public Property SomeProperty As String
    Private ReadOnly Property SomeProperty_ReadOnly As String Implements SomeInterface.SomeProperty
        Get
            Return Me.SomeProperty
        End Get
    End Property
End Class

The result is that SomeProperty is read-only when accessed through SomeInterface, but read-write when accessed through SomeClass:

Dim c As New SomeClass
c.SomeProperty = "hello" 'Write via class OK
Dim s1 = c.SomeProperty 'Read via class OK

Dim i As SomeInterface = c
Dim s2 = i.SomeProperty 'Read via interface OK
i.SomeProperty = "greetings" 'Syntax Error, interface property is read-only
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜