里面My.Settings定制类的ArrayList [英] Arraylist of custom classes inside My.Settings

查看:190
本文介绍了里面My.Settings定制类的ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Visual Basic .NET 2.0的程序。我是从一个较旧的设置文件移动的设置,一个app.config程序设置文件。我想这样做,因为很好地。

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.

所以,我说我的设置如下面的图片

在负载我这样做:

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

和,这是我如何使用它:

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

所以,问题是,我如何得到我的DatabaseConnectionSettings的数组列表保存到永久存储?我想做到这一点可能最彻底的方法。也就是说,我不希望有将其转换为一个字符串或将其保存到一个单独的文件中的每个时间我想用它。我想是能够使用My.Settings访问方法。

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.

推荐答案

调试+例外,勾选为CLR例外抛出框。现在你会看到什么错误。有几个例外,但交易断路器是第二个,类型......是没有预料到。

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

属性需要告诉什么类型存储在一个ArrayList的XM​​L序列化。这是在此 MSDN Library页面描述, 序列化一个ArrayList一节。问题是,你不能应用所需[的XmlElement]属性,因为ArrayList实例是自动生成的code声明。泛型列表(Of T)中不具有同样的问题,但现在你就揍成限制在设定的设计师,它不支持泛型类型。

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.

好了,一块石头,在这里一个坚硬的地方,你不能使这项工作。 StringCollection是令人反感的选择。或者用沟设置这样的想法,只是做自己用XML序列化。或者任何其他你preFER。

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.

这篇关于里面My.Settings定制类的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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