【问题标题】:Can I pass properties of a contained object without having to handle each one?我可以传递包含对象的属性而不必处理每个属性吗?
【发布时间】:2017-12-01 15:45:13
【问题描述】:

以这些类为例:

Public Class ClsA
    Public Property l As Integer
    Public Property w As Integer
    Public Property h As Integer
End Class


Public Class ClsB

    Private Property myClsA As ClsA

    Public Property l As Integer
        Get
            Return myClsA.l
        End Get
        Set(value As Integer)
            myClsA.l = value
        End Set
    End Property

    Public Property w As Integer
        Get
            Return myClsA.w
        End Get
        Set(value As Integer)
            myClsA.w = value
        End Set
    End Property

    Public Property h As Integer
        Get
            Return myClsA.h
        End Get
        Set(value As Integer)
            myClsA.h = value
        End Set
    End Property

    Public Property someOtherProperty As Integer
End Class

这意味着不是像这样访问“l”:

myClsB.myClsA.l

我可以的

myClsB.l

这是我想要的,但我必须单独通过每个属性,因此随着 ClsA 的公共属性数量的增加,它变得越工作。

有什么方法可以传递包含对象的所有属性(就好像它们是容器对象自己的属性一样)而不必单独处理每个属性?

【问题讨论】:

  • 目前没有,没有。即使对 DynamicObject 实现使用 dynamic 之类的操作也比编写包装器要困难得多。

标签: .net class properties


【解决方案1】:

Inherit From a Base Class

我认为获得您所追求的唯一方法是进行一些继承。请参阅上面的链接。

Public Class ClsA
    Public Property l As Integer
    Public Property w As Integer
    Public Property h As Integer
End Class


Public Class ClsB
    Inherits ClsA
End Class

用法:

Dim b As ClsB = New ClsB()
b.l = 1

【讨论】:

  • 谢谢。这将是我给出的简化示例的完美解决方案。不幸的是,我的实际类已经从另一个类继承。所以,我想我间接询问的是多重继承,我意识到 .net 框架中不存在多重继承。
猜你喜欢
  • 1970-01-01
  • 2018-12-25
  • 1970-01-01
  • 1970-01-01
  • 2020-07-28
  • 1970-01-01
  • 2015-12-23
  • 2013-10-01
  • 1970-01-01
相关资源
最近更新 更多