#region Copyright 2009-2014 by Roger Knapp, Licensed under the Apache License, Version 2.0 /* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #endregion using System; using System.Collections.Generic; using System.Configuration; namespace CSharpTest.Net.AppConfig { /// /// Provides a collection of name keyed sections that contain more key/value settings /// public sealed class UserSettingsSubSectionCollection : ConfigurationElementCollection { /// /// Constructs a collection of named sections /// public UserSettingsSubSectionCollection() : base(StringComparer.Ordinal) { base.AddElementName = "section"; base.ClearElementName = "clear"; base.RemoveElementName = "remove"; } /// /// Gets the type of the System.Configuration.ConfigurationElementCollection. /// public override ConfigurationElementCollectionType CollectionType { get { return ConfigurationElementCollectionType.AddRemoveClearMap; } } #region Protected Overrides /// creates a new UserSettingsSubSection protected override ConfigurationElement CreateNewElement() { return new UserSettingsSubSection(); } /// creates a new UserSettingsSubSection protected override ConfigurationElement CreateNewElement(string elementName) { UserSettingsSubSection secton = new UserSettingsSubSection(); secton.Name = elementName; return secton; } /// Returns the name of the UserSettingsSubSection protected override object GetElementKey(ConfigurationElement element) { return ((UserSettingsSubSection)element).Name; } #endregion /// /// Adds a new section with the specified name /// public UserSettingsSubSection Add(string name) { UserSettingsSubSection section = (UserSettingsSubSection)CreateNewElement(name); BaseAdd(section); return section; } /// /// Removes the specified collection by name /// public void Remove(string name) { BaseRemove(name); } /// /// Clears all elements from the collection /// public void Clear() { BaseClear(); } /// /// Returns the specified collection by name if it exists, or null if not found /// public new UserSettingsSubSection this[string name] { get { return (UserSettingsSubSection)BaseGet(name); } } /// /// Deep copy of all settings from one configuration to another. /// public void CopyFrom(UserSettingsSubSectionCollection other) { foreach (UserSettingsSubSection from in other) { UserSettingsSubSection to = this[from.Name]; if (to == null) to = this.Add(from.Name); to.CopyFrom(from); } } } }