序列化和搜索对象集合 [英] Serializing and Searching Object Collections

查看:117
本文介绍了序列化和搜索对象集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人可以回答关于存储和搜索对象集合的一些问题,我将不胜感激。

I would appreciate if somebody could answer some questions regarding storing and searching object collections please. If you could give a basic example of any suggestions I would be very grateful.

储存:

我会感谢您的建议。写一个应用程序,需要搜索Active Directory的计算机对象,我将它们存储在一个对象的集合。我想保存计算机对象集合以及未存储在Active Directory中的其他信息(例如计算机的MAC地址),以便下次运行应用程序。我还要保存OU的列表。

I'm writing an application which needs to search Active Directory for computer objects, and I'm storing them in a collection of objects. I want to save the computer objects collection along with additional information which is not stored in Active Directory (e.g. the computer's MAC address) for the next time the application is run. I also want to save a list of OU's.

这里是我的对象集合到目前为止(它将有更多的属性)。什么是保存集合的最好的方法?最好不要使用数据库或保存到文件会对性能产生巨大的影响?

Here is my object collection so far (It will have more properties). What is the best method of saving the collection? Preferably not using a database or will saving to a file have a drastic performance impact?

还是有更好的方法来做我下面的工作?

Or is there a better way to do what I have below?

Public Class Computer

    Public Property Name As String
    Public Property FQDN As String
    Public Property Path As String

End Class

Public Class ComputerCollection
    Inherits Collections.CollectionBase

    Public Sub Add(ByVal computer As Computer) 'Adds a new computer object to the collection
        List.Add(computer)
    End Sub

End Class

搜索:

我有一个类似于ADUC的布局,具有OU的树视图和显示计算机对象的列表视图在所选的OU中。以下代码通过计算机对象集合循环检查计算机对象的路径是否与所选OU路径匹配,然后在列表视图中显示它们。

I have a layout similar to ADUC with a tree view of OU's and a list view displaying computer objects in the selected OU. The following code loops through the computer object collection checking if the path of the computer object matches the selected OU path and then displays them in the list view.

这是最好的方法的性能?

Is this the best method in terms of performance? or is there a faster way?

Private Sub tvOU_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles tvOU.AfterSelect

    lvComputers.Items.Clear()

    If tvOU.SelectedNode.Name > "" Then

        For Each computerObj In computers

            If computerObj.Path.ToString.Replace("CN=" + computerObj.Name + ",", "") = tvOU.SelectedNode.Name Then
                Dim lvItem As New ListViewItem
                lvItem.Text = computerObj.Name
                lvComputers.Items.Add(lvItem)
            End If

        Next

    Else
        Exit Sub
    End If

End Sub


推荐答案

一个简单的列表(计算机)是一些其他未知的要求。使用这种类型的事物的序列化保存非常简单,并且对于 List Collection< T>

A simple List(Of Computer) is indeed all you need unless there is some other unknown requirement. Saving is very simple using serialization for this type of thing, and would work the same for a List or Collection<T>.

Imports System.Collections.ObjectModel

Public Class ComputersCollection
    Inherits Collection(Of Computer)
    ' NOT the crude thing in the VB NameSpace

    ' there is almost nothing to add: item, add and remove
    ' are in the base class
    ' ... perhaps saving and retrieving them by a key (name)
    ' which may do away with the search procedure in the posted code

End Class

Private Computers As New Collection(Of Computer)
' or
Private Computers As New List(Of Computer)

另一部分,保存你的集合,可以通过序列化简单。首先,你必须将Serializable属性添加到你的Computer类中:

The other part, saving your collection, can be simple with serialization. First, you will have to add the Serializable attribute to your Computer class:

<Serializable>
Public Class Computer

如果你忘记了,你会得到错误类foobar未标记为可序列化。保存到磁盘:

If you forget, you get the error Class foobar is not marked as serializable. Save to disk:

Imports System.Runtime.Serialization

Private myComputerFile As String = ...

Dim bf As New BinaryFormatter
Using fs As New FileStream(myComputerFile , FileMode.OpenOrCreate)
    bf.Serialize(fs, Computers)
End Using

然后,您可以轻松地重新创建列表或集合;这一次的列表:

Then you can recreate the List or Collection just as easily; this time the List:

' ToDo: add a check to skip if file is not there
' as in the first time run
Dim bf As New BinaryFormatter
Using fs As New FileStream(myComputerFile , FileMode.Open)
    Computers = CType(bf.Deserialize(fs), List(Of Computers))
End Using

或者,您可以使用XML序列化器创建XML文件, ,更智能的二进制序列化器是ProtoBuf-Net

Alternatively you can use the XML serializer to create an XML File or use the faster, smarter binary serializer is ProtoBuf-Net

这篇关于序列化和搜索对象集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆