My.Settings 中自定义类的数组列表

发布于 2024-09-28 01:11:43 字数 3486 浏览 1 评论 0原文

我有一个 Visual Basic .Net 2.0 程序。我正在将设置从较旧的设置文件移至 app.config 程序设置文件。我正在努力尽可能地做好这件事。

因此,我添加了我的设置如此图所示

在加载时,我这样做:

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 的数组列表保存到持久存储中?我想以尽可能最干净的方式做到这一点。也就是说,我不想每次使用它时都必须将其转换为字符串或将其保存到单独的文件中。我希望能够使用 My.Settings 访问器方法。

请注意,它工作正常,只是没有保存到存储中。

I have a Visual Basic .Net 2.0 program. I'm moving the settings from an older settings file, to an app.config program settings file. I'm trying to do this as nicely as possible.

So, I added my setting as shown in this image.

On load I do this:

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

This is my custom class:

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

And, this is how I use it:

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.'

So, the question is, how do I get that arraylist of my DatabaseConnectionSettings saved to persistent storage? I want to do this in the cleanest way possible. That is, I don't want to have to convert it to a string or save it to a separate file every-time I want to use it. I would like to be able to use the My.Settings accessor method.

Note, it's working perfectly, except it's not being saved to storage.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

递刀给你 2024-10-05 01:11:44

许多新手程序员,甚至高级程序员都不知道如何在“我的设置”中保存自定义类。

这是完美的解决方案,

  1. 您创建类并存储您想要存储的内容,在代码中的“我的设置
  2. ”中创建一个设置类型:数组列表
  3. ,当您想要保存时,您需要获取该类的内存流,序列化,使用格式化程序。

示例

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. 保存到数组列表中的 MYSETTINGS,添加 BYTE 类型的值()
    就像:ms.ToArray

这将起作用,并且当应用程序。 数组()现在仍将在我的设置中

  1. 它关闭并再次打开,当您想使用该值时,
    您需要使用格式化程序反序列化字节。

这是我创建的类,这有助于序列化、从您想要的任何对象中获取内存流以及反序列化。

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 aaron GmA IL doot com

MANY NOVICE PROGRAMMERS, EVEN ADVANCED ONES DOESNT KNOW HOW TO SAVE A CUSTOM CLASS IN MYSETTINGS.

HERE ITS THE PERFECT SOLUTION

  1. YOU CREATE THE CLASS AND SHIT YOU WANNA STORE
  2. CREATE A SETTING TYPE: ARRAYLIST IN MYSETTINGS
  3. IN CODE WHEN YOU WANT TO SAVE, YOU NEED TO GET MEMORYSTREAM OF THE CLASS, SERIALIZE, USE A FORMATTER.

EXAMPLE

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. SAVE TO MYSETTINGS IN ARRAYLIST, ADD VALUE OF TYPE BYTE()
    LIKE : ms.ToArray

THIS WILL WORK, AND WHEN THE APP. ITS SHUTDOWN AND OPENED AGAIN, THE ARRAY() WILL STILL BE IN MYSETTINGS

  1. NOW WHEN YOU WANT TO USE THE VALUE
    YOU NEED TO DESERIALIZE THE BYTES USING A FORMATTER.

HERE ITS THE CLASS I MADED, THIS HELPS TO SERIALIZE, GET MEMORYSTREAM FROM ANY OBJECT YOU WANT, AND DESERIALIZE.

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

IF YOU NEED SOME HELP OR HAVE QUESTION HERE ITS MY EMAIL:

bboyse aaron GmA IL doot com

硪扪都還晓 2024-10-05 01:11:43

调试 + 异常,勾选 CLR 异常的抛出框。您现在就会看到出了什么问题。有几个例外,但破坏交易的是第二个,“这种类型……不是预期的”。

需要属性来告诉 xml 序列化器 ArrayList 中存储的类型。 MSDN 库页面对此进行了描述, “序列化 ArrayList”部分。问题是,您无法应用所需的 [XmlElement] 属性,因为 ArrayList 实例是在自动生成的代码中声明的。泛型 List(Of T) 不存在相同的问题,但现在您会遇到设置设计器中的限制,它不支持泛型类型。

好吧,这里进退两难,你无法完成这项工作。 StringCollection 是令人讨厌的替代方案。或者放弃使用 Settings 来实现此目的的想法,而直接使用 xml 序列化来完成。或者任何你喜欢的其他东西。

Debug + Exceptions, tick the Thrown box for CLR exceptions. You'll now see what is going wrong. There are several exceptions but the deal breaker is the second one, "The type ... was not expected".

Attributes are required to tell the xml serializer what types are stored in an ArrayList. This is described in this MSDN Library page, "Serializing an ArrayList" section. Problem is, you cannot apply the required [XmlElement] attribute since the ArrayList instance is declared in auto-generated code. A generic List(Of T) doesn't have the same problem, but now you'll smack into a restriction in the setting designer, it doesn't support generic types.

Well, a rock and a hard place here, you can't make this work. StringCollection is the distasteful alternative. Or ditch the idea of using Settings for this and just do it yourself with xml serialization. Or whatever else you prefer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文