Arraylist of custom classes inside My.Settings

别等时光非礼了梦想. 提交于 2019-12-01 14:24:19

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.

Sergiu

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!