.Net如何在父对象标记为只读时使属性为只读

.Net如何在父对象标记为只读时使属性为只读,.net,vb.net,properties,scope,readonly,.net,Vb.net,Properties,Scope,Readonly,所以我有一个问题,我有一个对象,它包含了其他松散相关的对象。我只想让这个对象成为一种存储库,通过它可以读取变量,但如果使用这个对象,就不会改变变量。这是我的出发点(VB.Net): 现在,适当地,当我尝试设置对象本身时,如下所示: Dim context As New CompanyVendorContext(New Company, New Vendor) context.Company = New Company 它不允许我这样做,这是完美的。但是,当我尝试这样做时: Dim contex

所以我有一个问题,我有一个对象,它包含了其他松散相关的对象。我只想让这个对象成为一种存储库,通过它可以读取变量,但如果使用这个对象,就不会改变变量。这是我的出发点(VB.Net):

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

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对象访问时才可以?

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

Public Interface ICompany
    ReadOnly Property Id() As Integer
    ...
End Interface
当然,这里也需要注意一些。如果
Company
(实现
ICompany
的类)是可变的,那么没有什么可以阻止用户这样做:

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

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

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

当您需要使用这样的只读模式时,请使用接口

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

CompanyVendorContext的属性(单数)公司为只读。{roperty of Company是指属性在Company中的定义方式。
Company()
在VB中是一个数组。我知道:D.我的手指在做一些与大脑不同的事情
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