My.Settings 中自定义类的数组列表
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
许多新手程序员,甚至高级程序员都不知道如何在“我的设置”中保存自定义类。
这是完美的解决方案,
示例
就像:
ms.ToArray
这将起作用,并且当应用程序。 数组()现在仍将在我的设置中
您需要使用格式化程序反序列化字节。
这是我创建的类,这有助于序列化、从您想要的任何对象中获取内存流以及反序列化。
如果您需要帮助或有疑问,请发送我的电子邮件:
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
EXAMPLE
LIKE :
ms.ToArray
THIS WILL WORK, AND WHEN THE APP. ITS SHUTDOWN AND OPENED AGAIN, THE ARRAY() WILL STILL BE IN MYSETTINGS
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.
IF YOU NEED SOME HELP OR HAVE QUESTION HERE ITS MY EMAIL:
bboyse aaron GmA IL doot com
调试 + 异常,勾选 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.