BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Configuration
.NET 2.0+

Using Custom Classes with Application Settings

Application settings can be created using a project's property windows, with those values being transferred into configuration files and classes that simplify their use. The settings data can be of many different types, including custom classes.

Application Settings

The application settings designer in Visual Studio allows you to create configuration settings for your software in a simple grid layout. These settings are converted into classes and stored in a configuration file. You can access the values in the classes using static behaviour. The values are retrieved from the configuration file or, if not present in the file, default values are returned. When the software is deployed in a live environment the settings can be modified using a plain text editor without recompiling the code.

Many developers use application settings to add simple configuration items to their software. These generally include numeric or string values, including database connection strings. You can use more complex types, such as StringCollections and XmlDocuments. You can also create settings based upon your own custom classes with only minor modifications to those types.

There are two ways to prepare a class for use as an application setting. One allows the setting to be created using XML serialization. The other requires that you add code that converts between your class and the string that will appear in the configuration file. In this article we will see both methods.

Creating the Sample Project

To demonstrate the use of custom classes in application settings we need a test project. Create a new console application named, "SettingsTest" and add the two classes shown below. These are the types that we will modify for use with configuration files. The first represents an employee of a company. The second holds the details of a room within a building. In the sample code both use automatically implemented properties. If you are using .NET 2.0, you should expand these to full properties with backing store variables.

public class Employee
{
    public string Name { get; set; }
    public string Position { get; set; }
}


public class Room
{
    public int RoomNumber { get; set; }
    public string Location { get; set; }
}

Enabling a Class for Application Settings Using XML Serialization

The easiest way to prepare a class for use as an application setting is to indicate that it should be stored as XML in the configuration file. This method only requires that you apply an attribute to the class.

When a setting is configured in this way it must be defined using XML in the settings window or configuration file. This can be verbose in some cases but is often acceptable. As the standard XML serializer is used to read and write settings it is only suitable for use with classes that do not require private fields or properties to be stored.

To specify XML serialization we use an attribute from the System.Configuration namespace. Add the following using directive to simplify the code:

using System.Configuration;

The attribute that we need is SettingsSerializeAs. It should be placed before the class declaration with the argument indicating that we will serialize to XML. To add the attribute to the Employee class modify the code as follows. We will create an Employee setting later.

[SettingsSerializeAs(SettingsSerializeAs.Xml)]
public class Employee
{
    public string Name { get; set; }
    public string Position { get; set; }
}

Enabling a Class for Application Settings Using a TypeConverter

A more flexible approach than using XML serialization for settings is to create a type converter. This is a separate class that inherits from the TypeConverter class. It provides the functionality required to convert between a string and the type that you wish to use as a setting. The type converter is used whenever a setting is read from or written to a configuration file.

The advantage of a type converter is that you gain complete control of the process. You can store private values if required and overcome other limitations of XML serialization, whilst making the setting easy to change in the configuration file. The disadvantage is that you must create the type converter yourself so more code is required.

We will create a type converter for the Room class. We will be using types and attributes from the System.ComponentModel namespace so add the following using directive:

using System.ComponentModel;

We'll start by adding the attributes we need to the Room class. The SettingsSerializeAs attribute is still required but this time we specify that we will store the values as strings using the SettingsSerializeAs.String constant. We also need to specify the type converter that we will use by adding the TypeConverter attribute. In this case we'll be using the as yet unwritten RoomConverter.

[TypeConverter(typeof(RoomConverter))]
[SettingsSerializeAs(SettingsSerializeAs.String)]
public class Room
{
    public int RoomNumber { get; set; }
    public string Location { get; set; }
}
21 August 2011