In this article, you will learn how to utilize the powerful new .NET 2.0 configuration features to simplify and centralize your configuration code.
|
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
namespace Examples
{
public enum ArticleState
{
Completed,
InProgress,
Pending
}
public class ArticleElement: ConfigurationElement
{
#region Constructors
static ArticleElement()
{
s_propName = new ConfigurationProperty(
"name",
typeof(string),
null,
ConfigurationPropertyOptions.IsRequired
);
s_propDesc = new ConfigurationProperty(
"desc",
typeof(string),
null,
ConfigurationPropertyOptions.None
);
s_propState = new ConfigurationProperty(
"state",
typeof(ArticleState),
ArticleState.Pending,
ConfigurationPropertyOptions.None
);
s_propSequence = new ConfigurationProperty(
"sequence",
typeof(int),
0,
ConfigurationPropertyOptions.IsRequired
);
s_properties = new ConfigurationPropertyCollection();
s_properties.Add(s_propName);
s_properties.Add(s_propDesc);
s_properties.Add(s_propState);
s_properties.Add(s_propSequence);
}
#endregion
#region Fields
private static ConfigurationPropertyCollection s_properties;
private static ConfigurationProperty s_propName;
private static ConfigurationProperty s_propDesc;
private static ConfigurationProperty s_propState;
private static ConfigurationProperty s_propSequence;
#endregion
#region Properties
public string Name
{
get { return (string)base[s_propName]; }
set { base[s_propName] = value; }
}
public string Description
{
get { return (string)base[s_propDesc]; }
set { base[s_propDesc] = value; }
}
public ArticleState State
{
get { return (ArticleState)base[s_propState]; }
set { base[s_propState] = value; }
}
public int Sequence
{
get { return (int)base[s_propSequence]; }
set { base[s_propSequence] = value; }
}
protected override ConfigurationPropertyCollection Properties
{
get
{
return s_properties;
}
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Jon Rista has been programming since the age of 8 (first Pascal program), and has been a programmer since the age of 10 (first practical program). In the last 21 years, he has learned to love C++, embrace object orientation, and finally enjoy the freedom of C#. He knows over 10 programming languages, and vows that his most important skill in programming is creativity, even more so than logic. Jon works on large-scale enterprise systems design and implementation, and employs Design Patterns, C#, .NET, and SQL Server in his daily doings.