Additional Readonly Property in VB.Net Interface

I am trying to develop a simple interface that allows you to create quick lists from classes. Basically, the interface should return an identifier and a name. However, some classes have a computed read-only name property, others just use the read / write name property. Basically, I don't care if he has a getter, it doesn't matter if the setter has a property. How can I write this interface to handle or without throwing compilation errors?

I read this question and did not really follow it, maybe I'm just tight. If yes, please show me the error of my ways :)

+3
source share
1 answer

It seems that the answer from another question will work: here is a sample:

Public Interface IReadOnly
    ReadOnly Property Name() As String
End Interface

Public Interface IReadWrite
    Inherits IReadOnly

    Overloads Property Name() As String

End Interface

Public Class ReadOnlyClass
    Implements IReadOnly

    Private _Name
    Public ReadOnly Property Name() As String Implements IReadOnly.Name
        Get
            Return _Name
        End Get
    End Property
End Class

Public Class ReadWriteClass
    Implements IReadWrite

    Private ReadOnly Property ReadOnly_Name() As String Implements IReadOnly.Name
        Get
            Return Name
        End Get
    End Property

    Private _Name As String
    Public Overloads Property Name() As String Implements IReadWrite.Name
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property
End Class

The above approach will actually lead to classes that implement IReadWrite, also implementing IReadOnly - so you really need to disable IReadWrite to set the property.

Another approach that avoids this problem, but requires a bit more logic in the implementing classes and their caller, looks something like this:

Public Interface ISometimesWritable
    Property Name() As String
    ReadOnly Property AllowNameEdit() As Boolean
End Interface

Public Class ReadOnlyClass
    Implements ISometimesWritable

    Public ReadOnly Property AllowNameEdit() As Boolean Implements ISometimesWritable.AllowNameEdit
        Get
            Return False
        End Get
    End Property

    Private _Name As String
    Public Property Name() As String Implements ISometimesWritable.Name
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            Throw New NotSupportedException("Name cannot be set when AllowNameEdit is False")
        End Set
    End Property
End Class

Public Class ReadWriteClass
    Implements ISometimesWritable

    Public ReadOnly Property AllowNameEdit() As Boolean Implements ISometimesWritable.AllowNameEdit
        Get
            Return True
        End Get
    End Property

    Private _Name As String
    Public Property Name() As String Implements ISometimesWritable.Name
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property
End Class

Update: . To answer the question of reduction; "downcasting" is a term used to describe the casting of an object from a superclass, interface, or abstract base class Typeto a more specific one Type.

, : IReadOnly IReadWrite. , IReadWrite IReadOnly, , IReadWrite IReadOnly , IReadWrite.

IReadWrite IReadOnly, IReadWrite "" IReadOnly ( "" , , - ). IReadWrite IReadOnly, - IReadOnly IReadWrite.

, ReadWriteClass :

Public Sub SomeMethod()
    dim readOnlyInstance as IReadOnly = new ReadWriteClass()
    Console.WriteLine(readOnlyInstance.Name)

    ' The following line won't compile, since we're communicating with ReadWriteClass as an instance of IReadOnly
    'readOnlyInstance.Name = "Santa Clause"

    ' Here we downcast the variable to reference it by it other interface, IReadWrite
    dim readWriteInstance = DirectCast(readOnlyInstance, IReadWrite)

    ' Now we can both Get and Set the value of Name
    readWriteInstance.Name = "John Doe"
    Console.WriteLine(readWriteInstance.Name)

    ' Note that in the above example we created *one* instance of ReadWriteClass
    ' and have provided two variables / references to the same underlying object.
    Console.WriteLine(readOnlyInstance.Name) ' <-- note that this should return "John Doe"

End Sub
+5

Source: https://habr.com/ru/post/1716046/


All Articles