【问题标题】:.Net How to make properties readonly when parent object is marked as readonly.Net当父对象标记为只读时如何使属性只读
【发布时间】:2013-11-13 18:33:37
【问题描述】:

所以我有一个问题,我有一个对象,其中包含彼此松散相关的其他对象。我只希望这个对象成为一种可以读取变量的存储库,但如果使用此对象则不能更改。这是我的起点(VB.Net):

Public Class CompanyVendorContext
    Private _company As ICompany
    Private _vendor As IVendor

    Public ReadOnly Property Company As ICompany
        Get
            Return Me._company
        End Get
    End Property

    Public ReadOnly Property Vendor As IVendor
        Get
            Return Me._vendor
        End Get
    End Property

    Public Sub New(ByVal objCompany As ICompany, ByVal objVendor As IVendor)
        Me._company = objCompany
        Me._vendor = objVendor
    End Sub
End Class

现在,适当地,当我尝试设置对象本身时,如下所示:

Dim context As New CompanyVendorContext(New Company, New Vendor)
context.Company = New Company

它不允许我这样做,这是完美的。但是,当我尝试这样做时:

Dim context As New CompanyVendorContext(New Company, New Vendor)
context.Company.ID = 1

它允许我这样做。我可以将 Company 对象的属性设置为只读,但仅在从此 CompanyVendorContext 对象访问时?

【问题讨论】:

  • CompanyVendorContext 的属性(单数)公司是只读的。 {公司的属性是在公司中定义属性的方式。

标签: .net vb.net properties scope readonly


【解决方案1】:

ReadOnly 属性仅使该属性值只读;它不会影响属性引用的对象的行为。如果您需要创建一个真正的只读实例,则必须使 ICompany 不可变,如下所示:

Public Interface ICompany
    ReadOnly Property Id() As Integer
    ...
End Interface

当然,这里也需要注意。如果Company(实现ICompany的类)是可变的,那么没有什么可以阻止用户这样做:

CType(context.Company,Company).ID = 1

【讨论】:

    【解决方案2】:

    您还需要将 ID 属性设置为只读。

    【讨论】:

      【解决方案3】:

      假设您不想将属性的 set 访问器设置为私有或受保护,如果不更改 Company 类本身以使其所有属性为只读(并且在线上和下线),则没有简单的方法可以做到这一点。如果设计不允许您更改它,您可以编写某种适配器、代理或其他相关设计模式来包装每个属性的对象并且不允许设置这些属性。

      【讨论】:

        【解决方案4】:

        当你需要像这样使用只读时使用接口。

        Public Interface ICompanyVendorContext
            ReadOnly Property Company As ICompany
            ReadOnly Property Vendor As IVendor
        End Interface
        
        Public Class CompanyVendorContext Implements ICompanyVendorContext
        
            Private m_Company As ICompany
            Private m_Vendor As IVendor
        
            Public Property Company As ICompany
                Get
                    Return m_AppFolder
                End Get
                Set
                    m_AppFolder = Value
                End Set
            End Property
        
            Public Property Vendor As IVendor
                Get
                    Return m_Vendor
                End Get
                Set
                    m_Vendor = Value
                End Set
            End Property
        
            private readonly Property ReadonlyCompany As ICompany implements ICompanyVendorContext.Company
                Get
                    Return m_Company
                End Get
            End Property
        
            private readonly Property ReadonlyVendor As IVendor implements ICompanyVendorContext.Vendor
                Get
                    Return m_Vendor
                End Get
            End Property
        
        End Class
        

        【讨论】:

        • Company() 在 VB 中是一个数组。
        • 我知道:D。我的手指正在做一些与我的大脑分开的事情
        猜你喜欢
        • 2015-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-25
        • 2012-05-15
        • 2010-12-02
        相关资源
        最近更新 更多