代码之家  ›  专栏  ›  技术社区  ›  Michael T

My.Settings中自定义类的Arraylist

  •  0
  • Michael T  · 技术社区  · 14 年前

    我有一个Visual Basic.Net 2.0程序。我正在将设置从旧的设置文件移动到app.config程序设置文件。我正尽力做到最好。

    所以,我添加了我的设置 as shown in this image .

    加载时,我执行以下操作:

    If My.Settings.databaseConnectionSettings Is Nothing Then
                My.Settings.databaseConnectionSettings = New ArrayList()
    End If
    

    这是我的定制课程:

    Imports System.Xml.Serialization
    
    <Serializable()> Public Class DatabaseConnectionSettings
        Private _nickname As String = String.Empty
        Private _username As String = String.Empty
        Private _password As String = String.Empty
        Private _database As String = String.Empty
        Private _server As String = String.Empty
        Private _ssl As Boolean
    
        Public Sub New()
            _nickname = ""
            _username = ""
            _password = ""
            _database = ""
            _server = ""
            _ssl = False
        End Sub
    
        Public Sub New(ByVal nickname As String, ByVal username As String, _
                       ByVal password As String, ByVal database As String, _
                       ByVal server As String, ByVal ssl As Boolean)
            _nickname = nickname
            _username = username
            _password = password
            _database = database
            _server = server
            _ssl = ssl
        End Sub
    
        Public Property nickname() As String
            Get
                Return _nickname
            End Get
            Set(ByVal Value As String)
                _nickname = Value
            End Set
        End Property
    
        Public Property username() As String
            Get
                Return _username
            End Get
            Set(ByVal Value As String)
                _username = Value
            End Set
        End Property
    
        Public Property password() As String
            Get
                Return _password
            End Get
            Set(ByVal Value As String)
                _password = Value
            End Set
        End Property
    
        Public Property database() As String
            Get
                Return _database
            End Get
            Set(ByVal Value As String)
                _database = Value
            End Set
        End Property
    
        Public Property server() As String
            Get
                Return _server
            End Get
            Set(ByVal Value As String)
                _server = Value
            End Set
        End Property
    
        <XmlElementAttribute(ElementName:="ssl")> Public Property ssl() As Boolean
            Get
                Return _ssl
            End Get
            Set(ByVal Value As Boolean)
                _ssl = Value
            End Set
        End Property
    
    End Class
    

    我就是这么用的:

    Dim databaseSettings As New DatabaseConnectionSettings( _
                            Me.txtNickName.Text, Me.txtUser.Text, Me.txtPass.Text, Me.txtData.Text, _
                                Me.txtServer.Text, Me.chkSSL.Checked)
                            'This statement will increment the arraylist count'
                            My.Settings.databaseConnectionSettings.Add(databaseSettings)
                            'This statement will save everything but the array list'
                            My.Settings.Save()
                            'This statement reloads everything, but the array list.  The array list count after this goes to zero.'
                            My.Settings.Reload() 'If I remove this, program works fine until next run.'
    

    所以,问题是,如何将DatabaseConnectionSettings的arraylist保存到持久存储?我想以最干净的方式做这件事。也就是说,我不想每次想使用它时都把它转换成一个字符串或保存到一个单独的文件中。我希望能够使用My.Settings访问器方法。

    2 回复  |  直到 14 年前
        1
  •  2
  •   Hans Passant    14 年前

    调试+异常,勾选CLR异常的抛出框。你现在就知道怎么回事了。有几个例外,但交易破坏者是第二个,“类型。。。不是预期的”。

    属性用于告诉xml序列化程序ArrayList中存储了哪些类型。这是在这个描述 MSDN Library page

    好吧,一块石头和一个坚硬的地方,你不能让这工作。StringCollection是一个令人讨厌的选择。或者放弃使用设置来实现这一点的想法,只需自己使用xml序列化。或者其他你喜欢的。

        2
  •  2
  •   Nathan Rambeck    11 年前

    许多新手程序员,甚至是高级程序员都不知道如何在MYSETTINGS中保存自定义类。

    这是完美的解决方案

    1. 你创建了你想要存储的类和垃圾
    2. 在代码中,当你想保存时,你需要获取类的MEMORYSTREAM,序列化,使用格式化程序。

    例子

    Dim formatter As Runtime.Serialization.IFormatter = New Runtime.Serialization.Formatters.Binary.BinaryFormatter
                Dim ms As New IO.MemoryStream
                formatter.Serialize(ms, original)
                Return ms
    
    1. 比如: ms.ToArray

    1. 现在当你想使用这个值时

    这里是我编写的类,这有助于序列化、从任何对象获取MEMORYSTREAM以及反序列化。

    Public Class SerializableObjectCopier(Of ObjectType)
            Public Function GetMemoryStream(ByVal original As ObjectType) As IO.MemoryStream
                Dim formatter As Runtime.Serialization.IFormatter = New Runtime.Serialization.Formatters.Binary.BinaryFormatter
                Dim ms As New IO.MemoryStream
                formatter.Serialize(ms, original)
                Return ms
            End Function
            Public Function GetCopy(ByVal original As ObjectType) As ObjectType
                Dim formatter As Runtime.Serialization.IFormatter = New Runtime.Serialization.Formatters.Binary.BinaryFormatter
                Dim ms As New IO.MemoryStream
                formatter.Serialize(ms, original)
    
                ms.Seek(0, IO.SeekOrigin.Begin)
                Return CType(formatter.Deserialize(ms), ObjectType)
            End Function
            Public Function GetCopy(ByVal ms As System.IO.MemoryStream) As ObjectType
                Dim formatter As Runtime.Serialization.IFormatter = New Runtime.Serialization.Formatters.Binary.BinaryFormatter
                ms.Seek(0, IO.SeekOrigin.Begin)
                Return CType(formatter.Deserialize(ms), ObjectType)
            End Function
        End Class
    

    如果您需要帮助或有疑问,请发送我的电子邮件:

    bboyse亚伦GmA IL doot网站