>public abstract class BaseConfigurationSection<T> : ConfigurationSection > where T : ConfigurationSection, new() > { > public static T Open() > { > Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); > if (config.Sections[typeof(T).Name] == null) > { > T instance = new T(); > config.Sections.Add(typeof(T).Name, instance); > config.Save(ConfigurationSaveMode.Full); > } > return config.Sections[typeof(T).Name] as T; > } > > public void Save() > { > Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); > T section = (T)configuration.Sections[typeof(T).Name]; > CopyProperties(this, section); > configuration.Save(ConfigurationSaveMode.Full); > } > > private static void CopyProperties(ConfigurationElement source, ConfigurationElement destination) > { > foreach (PropertyInfo property in source.GetType().GetProperties()) > { > if (property.IsDefined(typeof(ConfigurationPropertyAttribute), false)) > { > if (typeof(ConfigurationElement).IsAssignableFrom(property.PropertyType)) > { > CopyProperties( > (ConfigurationElement)property.GetValue(source, null), > (ConfigurationElement)property.GetValue(destination, null)); > } > else > { > object value = property.GetValue(source, null); > property.SetValue(destination, value, null); > } > } > } > } > > } >>
>public class MySection : BaseConfigurationSection<MySection> > { > public MySection() > { > > } > > [ConfigurationProperty("Destination", DefaultValue = "Some place", IsRequired = true)] > public string Destination > { > get > { > return (string)this["Destination"]; > } > set > { > this["Destination"] = value; > } > } > > [ConfigurationProperty("DialCode", DefaultValue = "01932", IsRequired = true)] > public string DialCode > { > get > { > return (string)this["DialCode"]; > } > set > { > this["DialCode"] = value; > } > } >} >>
>class MySection2:MySection
>{
>}
>
>