所以我有一个问题,我有一个对象,其中包含彼此松散相关的其他对象。我只希望这个对象成为一种可以读取变量的存储库,但如果使用此对象则不能更改。这是我的起点(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 对象访问时才可以?