How can I make an interface property optionally read-only in VB.NET?

情到浓时终转凉″ 提交于 2020-01-04 09:16:53

问题


This is a follow-up to a previous question I had about interfaces. I received an answer that I like, but I'm not sure how to implement it in VB.NET.

Previous question:

Should this property be part of my object's interface?

public interface Foo{
  bool MyMinimallyReadOnlyPropertyThatCanAlsoBeReadWrite {get;}
}

How can I achieve this with the VB.NET syntax? As far as I know, my only option is to mark the property as ReadOnly (I cannot implement the setter) or not (I must implement the setter).


回答1:


Simply define the getter in one interface, and create a second interface that has both the getter and the setter. If your concrete class is mutable, have it implement the second interface. In your code that deals with the class, check to see that it is an instance of the second interface, cast if so, then call the setter.




回答2:


In VB.NET I would implement it this way:

Public Interface ICanBeSecure

    ReadOnly Property IsSecureConnection() As Boolean
End Interface

Public Interface IIsSecureable
    Inherits ICanBeSecure

    Shadows Property IsSecureConnection() As Boolean
End Interface


来源:https://stackoverflow.com/questions/240012/how-can-i-make-an-interface-property-optionally-read-only-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!