如何在VB.NET中将接口属性设置为只读为只读? [英] How can I make an interface property optionally read-only in VB.NET?

查看:813
本文介绍了如何在VB.NET中将接口属性设置为只读为只读?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我之前关于接口的问题的后续行动。我收到了一个我喜欢的答案,但我不确定如何在VB.NET中实现它。

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.

上一个问题:

该属性是否属于我的对象的一部分界面?

public interface Foo{
  bool MyMinimallyReadOnlyPropertyThatCanAlsoBeReadWrite {get;}
}

如何使用VB.NET语法实现这一目标?据我所知,我唯一的选择是将属性标记为ReadOnly(我无法实现setter)(我必须实现setter)。

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).

推荐答案

只需在一个界面中定义getter,然后创建一个同时具有getter和setter的第二个接口。如果您的具体类是可变的,请让它实现第二个接口。在处理类的代码中,检查它是否是第二个接口的实例,如果是,则调用setter。

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.

这篇关于如何在VB.NET中将接口属性设置为只读为只读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆