using System;
using System.Configuration;
using System.ComponentModel;
namespace AnticipatingMinds.PlatformServices.Configuration{
/// <summary>
/// Summary description for AppSettingsConfigurationValueAttribute.
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public sealed class AppSettingsConfigurationValueAttribute : ConfigurationValueStorageAttribute
{
public AppSettingsConfigurationValueAttribute(string valueName):this(valueName,null){}
public AppSettingsConfigurationValueAttribute(string valueName,object defaultValue)
{
this.valueName = valueName;
this.defaultValue = defaultValue;
}
public override void LoadValue(ConfigurationSection configurationSection, System.Reflection.PropertyInfo configurationValueProperty)
{
object propertyValue = DefaultValue;
string appSettingsValue = ConfigurationSettings.AppSettings[ValueName];
if(appSettingsValue != null)
{
TypeConverter converter = TypeDescriptor.GetConverter(configurationValueProperty.PropertyType);
propertyValue = converter.ConvertFromString(appSettingsValue);
}
configurationValueProperty.SetValue(configurationSection,propertyValue,new object[0]);
}
public override void SaveValue(ConfigurationSection configurationSection, System.Reflection.PropertyInfo configurationValueProperty)
{
TypeConverter converter = TypeDescriptor.GetConverter(configurationValueProperty.PropertyType);
string propertyValue = converter.ConvertToString(configurationValueProperty.GetValue(configurationSection,new object[0]));
ConfigurationSettings.AppSettings[ValueName] = propertyValue;
}
public string ValueName
{
get
{
return valueName;
}
}
public object DefaultValue
{
get
{
return defaultValue;
}
}
private string valueName;
private object defaultValue;
}
}
|