我有一个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访问器方法。
请注意,除了没有保存到存储空间外,它的工作正常。
答案 0 :(得分:2)
Debug + Exceptions,勾选CLR异常的Thrown框。你现在可以看到出了什么问题。有几个例外,但交易破坏者是第二个,“类型......不是预期的”。
需要使用属性来告诉xml序列化程序哪些类型存储在ArrayList中。这在MSDN Library page,“序列化ArrayList”部分中进行了描述。问题是,您无法应用所需的[XmlElement]属性,因为ArrayList实例是在自动生成的代码中声明的。通用List(Of T)没有相同的问题,但现在你会在设置设计器中遇到限制,它不支持泛型类型。
嗯,这里有一块岩石和一块坚硬的地方,你无法做到这一点。 StringCollection是令人反感的替代方案。或者放弃使用“设置”的想法,然后自己使用xml序列化。或者你喜欢的任何其他东西答案 1 :(得分:2)
许多NOVICE程序员,即使高级人员也不知道如何在冥冥中保存一个定制类别。
实施例
Dim formatter As Runtime.Serialization.IFormatter = New Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim ms As New IO.MemoryStream
formatter.Serialize(ms, original)
Return ms
ms.ToArray
这将起作用,当APP。它关闭并再次打开,阵列()将仍然在冥冥之中
在这里我已经完成了这个课程,这有助于精神化,从你想要的任何对象中获取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 aaron GmA IL doot com