我应该使用哪种.NET数据结构?

时间:2022-09-11 11:45:19

Here is the structure of the data my object will need to expose (the data is NOT really stored in XML, it was just the easiest way to illustrate the layout):

以下是我的对象需要公开的数据结构(数据实际上并不存储在XML中,它只是说明布局的最简单方法):

<Department id="Accounting">
  <Employee id="1234">Joe Jones</Employee>
  <Employee id="5678">Steve Smith</Employee>
</Department>
<Department id="Marketing">
  <Employee id="3223">Kate Connors</Employee>
  <Employee id="3218">Noble Washington</Employee>
  <Employee id="3233">James Thomas</Employee>
</Department>

When I de-serialize the data, how should I expose it in terms of properties on my object? If it were just Department and EmployeeID, I think I'd use a dictionary. But I also need to associate the EmployeeName, too.

当我对数据进行反序列化时,我应该如何在对象的属性方面公开它?如果它只是Department和EmployeeID,我想我会使用字典。但我也需要关联EmployeeName。

4 个解决方案

#1


Class Department
   Public Id As Integer
   Public Employees As List(Of Employee)
End Class

Class Employee
   Public Id As Integer
   Public Name As String
End Class

Something like that (can't remember my VB syntax). Be sure to use Properties versus Public members...

这样的事情(不记得我的VB语法)。务必使用属性与公共成员...

#2


A Department class (with ID and Name), which contains a collection of Employee (ID and Name as well) objects.

Department类(带ID和Name),包含Employee(ID和Name)对象的集合。

#3


Public Class Employee

    Private _id As Integer
    Public Property EmployeeID() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property

    Private _name As String
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property


End Class

Public Class Department

    Private _department As String
    Public Property Department() As String
        Get
            Return _department
        End Get
        Set(ByVal value As String)
            _department = value
        End Set
    End Property

    Private _employees As List(Of Employee)
    Public Property Employees() As List(Of Employee)
        Get
            Return _employees
        End Get
        Set(ByVal value As List(Of Employee))
            _employees = value
        End Set
    End Property

End Class

#4


  • A department object
  • 部门对象

  • An employee object
  • 员工对象

  • An employeeCollection object. (optional, you can just use List(of Employee))
  • employeeCollection对象。 (可选,您只需使用List(员工))

Mark them all as serializable, and then you can (de)serialize them to whatever format you like.

将它们全部标记为可序列化,然后您可以(de)将它们序列化为您喜欢的任何格式。

#1


Class Department
   Public Id As Integer
   Public Employees As List(Of Employee)
End Class

Class Employee
   Public Id As Integer
   Public Name As String
End Class

Something like that (can't remember my VB syntax). Be sure to use Properties versus Public members...

这样的事情(不记得我的VB语法)。务必使用属性与公共成员...

#2


A Department class (with ID and Name), which contains a collection of Employee (ID and Name as well) objects.

Department类(带ID和Name),包含Employee(ID和Name)对象的集合。

#3


Public Class Employee

    Private _id As Integer
    Public Property EmployeeID() As Integer
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property

    Private _name As String
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property


End Class

Public Class Department

    Private _department As String
    Public Property Department() As String
        Get
            Return _department
        End Get
        Set(ByVal value As String)
            _department = value
        End Set
    End Property

    Private _employees As List(Of Employee)
    Public Property Employees() As List(Of Employee)
        Get
            Return _employees
        End Get
        Set(ByVal value As List(Of Employee))
            _employees = value
        End Set
    End Property

End Class

#4


  • A department object
  • 部门对象

  • An employee object
  • 员工对象

  • An employeeCollection object. (optional, you can just use List(of Employee))
  • employeeCollection对象。 (可选,您只需使用List(员工))

Mark them all as serializable, and then you can (de)serialize them to whatever format you like.

将它们全部标记为可序列化,然后您可以(de)将它们序列化为您喜欢的任何格式。