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 class ArticlesElementCollection: ConfigurationElementCollection
{
#region Constructor
public ArticlesElementCollection()
{
}
#endregion
#region Properties
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMap;
}
}
protected override string ElementName
{
get
{
return "article";
}
}
protected override ConfigurationPropertyCollection Properties
{
get
{
return new ConfigurationPropertyCollection();
}
}
#endregion
#region Indexers
public ArticleElement this[int index]
{
get
{
return (ArticleElement)base.BaseGet(index);
}
set
{
if (base.BaseGet(index) != null)
{
base.BaseRemoveAt(index);
}
base.BaseAdd(index, value);
}
}
public ArticleElement this[string name]
{
get
{
return (ArticleElement)base.BaseGet(name);
}
}
#endregion
#region Methods
public void Add(ArticleElement item)
{
base.BaseAdd(item);
}
public void Remove(ArticleElement item)
{
base.BaseRemove(item);
}
public void RemoveAt(int index)
{
base.BaseRemoveAt(index);
}
#endregion
#region Overrides
protected override ConfigurationElement CreateNewElement()
{
return new ArticleElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return (element as ArticleElement).Name;
}
#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.