In the previous article, we saw how to use PostSharp to help us to use the configuration sections, with the use of two aspects. In this article, I will focus on the same objective. But this time, we will use only one aspect, which will be a mix of the two aspects of the last article.
Tag - ConfigurationSection
Tuesday 7 December 2010
[PostSharp] Ease the creation of configuration sections - Update
By Michael DELVA on Tuesday 7 December 2010, 09:56 - C#
Wednesday 1 December 2010
[PostSharp] Ease the creation of configuration sections
By Michael DELVA on Wednesday 1 December 2010, 14:00 - C#
In this article, I'll show yet another use of PostSharp, this time focusing on the ConfigurationSection class. If you look in the MSDN at the articles which explain how to use the ConfigurationSection, you may have noticed that the code you need to write is repetitive, tedious, and error-prone:
public class PageAppearanceSection : ConfigurationSection
{
[ConfigurationProperty("remoteOnly", DefaultValue = "false", IsRequired = false)]
public Boolean RemoteOnly
{
get { return (Boolean)this["remoteOnly"]; }
set { this["remoteOnly"] = value; }
}
[ConfigurationProperty("font")]
public FontElement Font
{
get { return (FontElement)this["font"]; }
set { this["font"] = value; }
}
[ConfigurationProperty("color")]
public ColorElement Color
{
get { return (ColorElement)this["color"]; }
set { this["color"] = value; }
}
}
As you can see, you have to put the ConfigurationPropertyAttribute on top of each property, and to make calls to an indexer to the Get and Set methods of the properties, using the same name you used in the attribute.
Doesn't this repetition look like to be a good candidate for an aspect?