« Back to Log

Custom application settings

Posted by myself on June 28, 2008

Within a c# winform application I needed to store a generic collection to my settings, which I found out was not possible out of the box. It is possible though to set the required type in the properties designer dialog, but the serialization's output is always nothing.

On my research on the internet for a solution i stumbled upon the post Tips for C# .NET Software Developers, which described that the attribute [SettingsSerializeAs(SettingsSerializeAs.Binary)] was required for the settings property, and to set it accordingly in the Settings.Designer.cs file. The problem here is that modifying designer generated code file is not the best of all ideas, as the manual changes are overwritten every time the settings are modified by the designer. Thus, I followed the msn article on how to create application settings, which told me to create a custom settings class. The drawback with this solution is that I now have two settings classes, and not only one single to cover it all.

Here's an example of the settings class. Note the use of the SettingsSerializeAs attribute, as it is the key to the successful serialization.


using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace Koffeinfrei
{
    /// <summary>
    /// This class provides extended settings. Use this class for types that need to have special
    /// attributes for serialization, that cannot be set using the config designer dialog.
    /// </summary>
    internal class ExtendedSettings : ApplicationSettingsBase
    {
        private static ExtendedSettings defaultInstance = 
            ((ExtendedSettings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new ExtendedSettings())));

        /// <summary>
        /// Returns the default instance of this class.
        /// </summary>
        /// <remarks>Get the value of the <see name="defaultInstance"/> member.</remarks>
        public static ExtendedSettings Default
        {
            get
            {
                return defaultInstance;
            }
        }

        /// <summary>
        /// The <see cref="SomeCustomClass"/>s stored in the user scoped settings.
        /// </summary>
        /// <remarks>Get / set the value of the <c>CustomClasses</c> property.</remarks>
        [global::System.Configuration.UserScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.SettingsSerializeAs(
            global::System.Configuration.SettingsSerializeAs.Binary)]
        public global::System.Collections.Generic.Queue<SomeCustomClass> CustomClasses
        {
            get
            {
                return ((global::System.Collections.Generic.Queue<SomeCustomClasses>)(this["CustomClasses"]));
            }
            set
            {
                this["CustomClasses"] = value;
            }
        }
    }
}

No comments registered

Random Quote

Ich bin durchaus nicht zynisch, ich habe nur meine Erfahrungen, was allerdings ungefähr auf dasselbe hinauskommt. (Oscar Wilde)

» ...

koffeinfrei on identi.ca

icon
2010-07-26 11:46:49
Everybody needs a fishbowl: “YouTube - Barry Schwartz: The paradox of choice” — http://www.youtube.com/watch?v=VO6XEQIsCoM
icon
2010-06-18 09:07:19
@kentbrew: what i'm i missing? the #identica-badge doesn't show my entries on http://koffeinfrei.org/
icon
2010-06-16 08:00:30
“YouTube - BP Spills Coffee” — http://www.youtube.com/watch?v=2AAa0gd7ClM
icon
2010-06-01 20:03:29
"The surprising truth about what motivates us”: http://www.youtube.com/watch?v=u6XAPnuFjJc&feature=player_embedded
icon
2010-05-20 21:11:48
“Sumatra PDF is a slim, free, open-source PDF viewer for Windows” — http://blog.kowalczyk.info/software/sumatrapdf/index.html

Ying and Yang

where I am
where I should be