.net 如何避免由Friend属性引起的间接引用?
取三个VB.net程序集:A、B和C。A引用B,B引用C,但A不引用C。给定以下三个类,每个程序集中一个: 大会A.net 如何避免由Friend属性引起的间接引用?,.net,vb.net,access-modifiers,.net,Vb.net,Access Modifiers,取三个VB.net程序集:A、B和C。A引用B,B引用C,但A不引用C。给定以下三个类,每个程序集中一个: 大会A Public Class View() Public Property ViewModel as SomeViewModel End Class 大会B Public Class SomeViewModel() 'Compiler is ok with private variable _Model, doesn't complain. Privat
Public Class View()
Public Property ViewModel as SomeViewModel
End Class
大会B
Public Class SomeViewModel()
'Compiler is ok with private variable _Model, doesn't complain.
Private _Model as SomeModel
Friend Sub New(Model as SomeModel)
Me._Model = Model
End Sub
'Compiler complains when Friend property is added, requiring A to directly
'reference C.
Friend ReadOnly Property UnderlyingModel as SomeModel
End Class
装配C
Public Class SomeModel()
Public Property ModelID as Integer
End Class
本质上,程序集C包含一个模型SomeModel,该模型由程序集B中的SomeViewModel包装。SomeViewModel通过Friend属性向AssemblyB中的其他类公开该模型。部件A中的视图包含SomeViewModel,但由于友元修改器,视图无法访问SomeViewModel.UnderlineModel属性
鉴于此配置,VS 2010在编译程序集A时会出现以下编译错误:
Error 3 Construct makes an indirect reference to project 'AssemblyC',
which contains 'AssemblyC.SomeModel'. Add a project reference to 'AssemblyC' to your
project.
有没有一种方法可以通过友元修饰符公开UnderlyingModel属性,而不使用程序集引用程序集C?我原以为Friend修饰符可以实现这一点,因为程序集A中的视图无法访问在程序集C中公开模型的属性?将属性定义为Friend与否并不重要
当编译器构建程序集A时,它会找到在程序集B中定义的类型。我猜您创建了项目引用,因此它也构建了程序集B。现在,程序集B需要程序集C才能工作,没有它您无法使用解决方案。因此,此程序集也必须对编译器可用。我想它可以作为文件参考,但如果它是您自己的代码,那么在一天结束时更方便地一致使用项目参考。C由B引用项目参考,因此,当编译器构建B时,它应该构建C。Visual Studio要求A直接引用C。在添加Friend属性之前,我在ViewModel B中有大量引用程序集C的私有变量,但编译器没有抱怨它们,只有Friend属性。我将更新我的代码以显示这一点。我无法真正复制您的场景。这一切都适用于我,没有编译错误……当然,真正的应用程序比这里的示例更复杂。在实际应用程序中,视图使用绑定源Windows窗体绑定到视图模型。绑定源或Windows窗体设计器本身可能有一些有趣的地方。我会看看我是否能用我的示例代码重现这个问题,然后报告回来。