User setting of type Object[] not saving-Collection of common programming errors
msdn Hi,I am using VS2005 and I need to store a collection of custom objects as part of my application.Since we can’t use Generics as a settings type I did create a user-scope setting with a type of System.Object[] and cast to and from it at runtime.Everything works fine at runtime except I don’t seem to be able to save this particular setting (all others work fine).Does VS2005/.net2.0 support settings of this type (it allows me to declare them) ?If not, what approach would you suggest for such a situation ? Itried using an ArrayList with the same result.Many thanks,MetaMeta MetaMeta-
10 Answers
msdn1The application settings were never meant to store complex types, however you can serialize your type and then convert the stream to base64 string and feed that to the settings store. The following two methods can convert any object to and from a base64 string:
public static string ConvertToBase64(object graph) { if (graph == null) throw (new ArgumentNullException("graph")); MemoryStream ms = new MemoryStream(); IFormatter formatter = new BinaryFormatter(); formatter.Serialize(ms, graph); return Convert.ToBase64String(ms.ToArray(), Base64FormattingOptions.InsertLineBreaks); } public static object ConvertFromBase64(string s) { MemoryStream ms = new MemoryStream(Convert.FromBase64String(s)); IFormatter formatter = new BinaryFormatter(); return formatter.Deserialize(ms); }
/Calle
msdn2 The items in the collection might require default parameterless constructors. The collection is the primary type being serialized.The items are secondary, but must still be serializable, too.
msdn3 i dont know much about saving in xml. But for writing objects to file the best way is to serialize and write. Else you need to manually write the data by going through the attributes of the object.
msdn4The application settings were never meant to store complex types, however you can serialize your type and then convert the stream to base64 string and feed that to the settings store. The following two methods can convert any object to and from a base64 string:
public static string ConvertToBase64(object graph) { if (graph == null) throw (new ArgumentNullException("graph")); MemoryStream ms = new MemoryStream(); IFormatter formatter = new BinaryFormatter(); formatter.Serialize(ms, graph); return Convert.ToBase64String(ms.ToArray(), Base64FormattingOptions.InsertLineBreaks); } public static object ConvertFromBase64(string s) { MemoryStream ms = new MemoryStream(Convert.FromBase64String(s)); IFormatter formatter = new BinaryFormatter(); return formatter.Deserialize(ms); }
/Calle
msdn5 Calle,That looks like what i need. before I jump in, could you please tell me if :1- It works for collections of complex types : do i have to serialize each object in the collection in turn and then store it in a StringCollection for example or can I pass the collection directly to your converter store it as a sinle string ?2-It works for complex types whose attributes are other complex types or enums ?Thanks,MetaMeta
msdn6 If each object in the collection can be serialized, then no problem. Bottom line is each object can be serialized then only it will work.
msdn7 The items in the collection might require default parameterless constructors. The collection is the primary type being serialized.The items are secondary, but must still be serializable, too.
msdn8 1 – You can pass a complete object graph as long as every object being serialized has the Serializable attribute.2 – Sorry?Bottom line is that the same rules apply to this as for general serialization. That’s the whole idea. The application settings will never be aware of the data being stored. My only recommendation is to not serialize more than necessary as it will clutter up your settings file. Since the settings file is an XML file is can be opened outside your application and then it’s nice if its not to messy…/Calle
msdn91 – You can pass a complete object graph as long as every object being serialized has the Serializable attribute.2 – Sorry?Bottom line is that the same rules apply to this as for general serialization. That’s the whole idea. The application settings will never be aware of the data being stored. My only recommendation is to not serialize more than necessary as it will clutter up your settings file. Since the settings file is an XML file is can be opened outside your application and then it’s nice if its not to messy…
/Calle
msdn10 It all works fine using Calle’s code snippet and keeping in mind Rudedog2 comments.Thanks a bunch!Cheers,