首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >处理嵌套列表对象属性

处理嵌套列表对象属性
EN

Stack Overflow用户
提问于 2014-07-25 07:50:42
回答 1查看 95关注 0票数 0

大家好,我正在设法找出有效的方法来处理这些嵌套的列表对象,在本例中,从XML反序列化。例如,这个模型结构

代码语言:javascript
复制
public class Car 
   public property wheels as List(Of Wheels)
   public property BearingType as String 
end class

public class Wheels
   public property bearings as List(Of Bearings)
end class

public class Bearings
   public property BearingType as String 
end class

假设我像这样反序列化了2个Car对象

代码语言:javascript
复制
Car 1 (
Wheel 1 (Bearing 1 (BearingType Alloy) , Bearing 2(BearingType Alloy)
Wheel 2 (Bearing 3 (BearingType Alloy) , Bearing 4(BearingType Alloy)
)

Car 2(
Wheel 1 (Bearing 1 (BearingType Metal) , Bearing 2(BearingType Metal)
Wheel 2 (Bearing 3 (BearingType Metal) , Bearing 4(BearingType Metal)
)

因为我确信一辆车只会有一种类型的BearingType,我如何将轴承类型分配给父级?这样我就可以通过Car.BearingType而不是Car.Wheels(0).Bearings(0).BearingType来访问它。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-25 10:26:49

那么XML-结构是由其他人定义的吗?那么我就留着它,如果你是舒尔,它就只有一个,那么你就能解决这个既快又脏的问题:

代码语言:javascript
复制
Public Class Car
    Public Property wheels As List(Of Wheels)
    Public Property BearingType As String
        Get
            Return Me.wheels(0).bearings(0).BearingType()
        End Get
        Set(value As String)  
            For Each wheel In Me.wheels
                For Each bearing In wheel.bearings
                    bearing.BearingType = value
                Next
            Next
        End Set
    End Property
End Class

或者更好:实现IXmlSerializable。

否则,重新格式化xml文件并更新类。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24950632

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档