受保护的设置在VB.Net在接口中定义的属性 [英] Protected Set in VB.Net for a property defined in an interface

查看:121
本文介绍了受保护的设置在VB.Net在接口中定义的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个接​​口,其可以严重简化为:

We have an interface, which can be grossly simplified as:

public interface IPersistable<T>
{
    T Id { get; }
}

这是实现该接口的大多数地方想拥有它,以便有在该属性,即受保护的或私有集合,在C#:<​​/ P>

Most places that implement the interface want to have it so that there is a protected or private set on that property, i.e, in C#:

public class Foo : IPersistable<int>
{
    public int Id { get; protected set; }
}

不过,我不能让任何样品VB.Net code编译遵循相同的模式,同时还实现了接口,因此:

However, I can't get any sample VB.Net code to compile that follows the same pattern, whilst still implementing the interface, so:

Public Class Foo
    Implements IPersistable(Of Integer)

    Public Property Id() As Integer Implements IPersistable(Of Integer).Id
        Get
            Throw New NotImplementedException()
        End Get
        Protected Set(ByVal value As Integer)
            Throw New NotImplementedException()
        End Set
    End Property
End Class

...不会编译,但这:

...will not compile, but this would:

Public Class Foo
    Public Property Id() As Integer
        Get
            Throw New NotImplementedException()
        End Get
        Protected Set(ByVal value As Integer)
            Throw New NotImplementedException()
        End Set
    End Property
End Class

我AP preciate,这个例子过于琐碎,而且将可能通过保护构造更好地实现,但我很感兴趣,如果能以这种方式来完成?

I appreciate that this example is overly trivial, and would possibly be better achieved through protected constructors, but I'm interested if it can be done in this manner?

...,显然,如果一个类型想要使用XMLSerialization,那么属性将需要公共读/写或类型将需要对每个写入定制序列

[ ] ...and obviously, if a type wants to use XMLSerialization, then the properties would need to be public read/write or the types would require custom serializers written for each.

从本质上讲,我看到它的接口应该确定最低可达性,但VB是跨preting它的确切访问?

Essentially, I see it that the interface should define the minimum accessibility, but VB is interpreting it as the exact accessibility?

推荐答案

是的,你必须从字面上实现的接口。一个可能的解决方法是重新发布在类的属性与另一个名称:

Yes, you'll have to implement the interface literally. A possible workaround is to republish the property in the class with another name:

Public Class Foo
  Implements IPersistable(Of Integer)
  Private m_Id As Integer

  Public ReadOnly Property Id() As Integer Implements IPersistable(Of Integer).Id
    Get
      Return m_Id
    End Get
  End Property

  Protected Property IdInternal() As Integer
    Get
      Return m_Id
    End Get
    Set(ByVal value As Integer)
      m_Id = value
    End Set
  End Property
End Class

申报财产可重写,如果你打算重写它的派生类。

Declare the property Overridable if you intend to override it in derived classes.

这篇关于受保护的设置在VB.Net在接口中定义的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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