How To Serialize an Object using application settings

In my application, I need the ability to store user defined data. My desire is to use the new application Settings mechanism (.NET 2.0)

I have been experimenting with the various methods of doing this...

Initially I used the IDE's application\properties\settings method where you specificy the settings in the gridview and VisualStudio creates the wrapper class for you. I had success doing this but when I tried to serialize the data inside one of my application's objects (I created a MRUList class) I had difficulty. I was expecting that you click "Type", select "Browse" and you would see the application namespace and all underlying classes. Not so (or am I missing something). Is there something you have to do to make your namespace visible in the browse dialog

Next I tried creating my own wrapper class based on the ApplicationSettingsBase. Again I had success with predefined classes (like Point) but still could not figure out how to make it so I could serialize the data from one of my objects.

In doing some hunting, I found information on making a custom control capable of persisting itself using IPersistComponentSettings. Does an object need to implement this in order serialize Or does this just apply to components.

The other thing I was looking at was Type Convertors. I noticed the Point class has a type convertor to convert a string to its coordinates. (I assume the ToString() method converts its data to the "x, y" string.) How do type convertors factor in serializing objects into the application settings.

Hopefully I am missing somthing fairly simple as I am somewhat new to the .NET framework. Any help would me much appreciated.



Answer this question

How To Serialize an Object using application settings

  • Mike12345

    Thanks Peter,

    I Implemented a type converter to see how that worked... I created a fairly simple class to test it. It worked well.

    My next experiment will be to use an XML serializer. I think this may be the preferred method for more complex objects. (The converter method I used would in the end just create a really long string would require an increasingly complex parser for increasingly complex objects.)

    I also noticed in my experiments, how the framework stores the arraylist type. It uses some form of XML schema. I have no experience with XML; is the arraylist just using the standard XML serialization mechanism I will look for articles on XML serialization but if you can suggest one that explains it in a beginner context it would be well appreciated.

    One other thing... so far as adding items to the application settings through the Project's\properties\settings window in VS2005... Is it at all possible to access the objects within your project through the Type\browse path It appears all objects from every namespace under System and Microsoft is available... what if I wanted to add my class library to it Is it even possible

    Thanks again for your help.


  • Pvanroos

    Hello, some more info on the subject.

    Today I found two of my components showing up when browsing for types. I started wondering why these two and not others.

    1. I use these components in my project (they appear under references).

    2. The components are already compiled and installed in the global assembly cache using NGEN !!!

    If it uses assemblies from the GAC (as it seems), I really think that is bad. I mean, who says, they will be present in the GAC on the target computer Do we have to install our assemblies in the GAC prior to using the ApplicationSettings with our own types

    I would be really nice if someone from Microsoft could clarify this. It's very poorly documented.

    BTW: My types worked fine, I could save and load the types.



  • markomar

    Hi Greg,

    Where did you define the class of the Customer Object
    Based on my test, if I have a winform application(WindowsApplication9), and I add a class file(Class1.cs). So the class will be built into the WindowsApplication9.exe.
    In the Object Browser, the Class1 will under the WindowsApplication9 project.
    NOTE: the WindowsApplication9 project have a different icon than the other Assembly icon.

    If you still have any concern, please feel free to post here.

    Best regards,
    Peter Huang



  • Joe McGrath

    I never found a method to access my custom objects through this mechanism. Instead I opted to create my own Usersettings class (Inheriting the ApplicationSettingsBase). In the end I think this gives you more control over the object's behaviour so I stopped looking.

    If you do find a means to do this let me know for interest sake!

    -Greg


  • jinjun

    Hi,

    Currently I am researching the issue and we will reply here with more information as soon as possible.
    If you have any more concerns on it, please feel free to post here.

    Thanks for your understanding!

    Best regards,

    Peter Huang



  • Charlie_MS

    Peter,

    All my objects show up in the object browser as well.

    What Thoumas and I are having difficulty with is using the property settings page to add our objects to the application settings so they get serialized.

    Right click on the project in the solution Explorer, then select the Settings side menu item... you will see a grid where you can add things to property settings... things like window location, size and the like. We would like to be able to add our custom objects to this grid... however, when you try to find them under in the grid combobox "Type" you cannot find any reference to the assembly's objects. The only hopeful item is browse but that does not get you want you want either.

    It is more than likely I am missing something simple... an attribute to the top of any class you want visible or perhaps you just have create a class library and add it to a specific folder... However, I have yet to find any documentation on it and like I stated in my other post I have decided to use another approach anyway.

    Thanks for you input.

    -Greg


  • Critcho

    Hi,

    Based on my understanding, here is the resource for your reference.

    Q: How are my strongly typed properties serialized as settings I couldn't get the <insert type here> class to serialize correctly.

    A: There are two primary mechanisms that ApplicationSettingsBase uses to serialize settings - (1) If a TypeConverter exists that can convert to and from string, we use it. (2) If not, we fallback to the XmlSerializer. While most common types can be serialized in one of these ways, there are some types that may not. In such cases, you have a few different options:

    Implement a TypeConverter for the type that can convert to and from string. The implementation can use a suitable serialization mechanism like one of the formatters/serializers that ship in the Framework or any custom mechanism you wish. You can then specify this converter on the type itself or on the property in your settings class.
    Specify a particular SettingsSerializeAs enum value using a SettingsSerializeAsAttribute. For example, if you wish to serialize a setting in binary format, simply specify SettingsSerializeAs.Binary.
    http://blogs.msdn.com/rprabhu/articles/433979.aspx

    For the Size, Point that working, turns out lots of types in the Framework have type converters, but especially types that are properties on components that will be hosted on design surfaces such as the WinForms designer, because type converters to/from string are used by the property grid to provide stringified forms of the property values.

    From the blogs, note that “there are some types that may not” be able to use the XML serialization fallback – the most common reason that a type can’t be serialized is that there isn’t a default (parameterless) constructor for the type.


    Best regards,
    Peter Huang



  • Srreh

    I have the same problem, I think.

    Exactly what needs to be done in order to get a custom type to appear in Project's Properties->Settings->Type->Browse..->Select a Type -list

    Or is this possible at all, can the list show anything but "well-known" types


  • Brian Stringham

    Hi Greg,

    Thanks for your clarification.
    I can reproduce your scenario.
    That is a good idea to add the feature into IDE.
    I do understand your concern in this scenario, I highly suggest you can
    submit this feedback to our product feedback center:
    http://lab.msdn.microsoft.com/productfeedback/default.aspx

    So far I think you may try to hard code the full qualified name of the Type in the textbox when you choose the type.
    e.g. namespace1.namespace2.classname

    Thanks for your understanding!
    If you still have any concern, please feel free to post here.

    Best regards,
    Peter Huang



  • How To Serialize an Object using application settings