Пример #1
0
        /// <summary>
        /// Processes all duplicate sections within this collection according to the specified behavior.
        /// </summary>
        /// <param name="behavior">An <see cref="IniDuplicateSectionNameBehavior" /> object specifying how duplicates are processed.</param>
        /// <param name="ignoreCase"><see langword="true" /> to ignore character casing during name comparison.</param>
        public void ProcessDuplicates(IniDuplicateSectionNameBehavior behavior, bool ignoreCase)
        {
            List <IniSection> removedSections = new List <IniSection>();

            switch (behavior)
            {
            case IniDuplicateSectionNameBehavior.Abort:
                if (Sections.DistinctBy(section => ignoreCase ? section.Name.ToLower() : section.Name).Count() != Count)
                {
                    throw Throw.Format("Duplicate section found.");
                }
                break;

            case IniDuplicateSectionNameBehavior.Ignore:
                for (int i = 1; i < Count; i++)
                {
                    if (Sections.Take(i).Any(section => section.Name.Equals(Sections[i].Name, ignoreCase ? SpecialStringComparisons.IgnoreCase : SpecialStringComparisons.None)))
                    {
                        removedSections.Add(Sections[i]);
                    }
                }
                break;

            case IniDuplicateSectionNameBehavior.Merge:
                for (int i = 1; i < Count; i++)
                {
                    IniSection firstSection = Sections.Take(i).FirstOrDefault(section => section.Name.Equals(Sections[i].Name, ignoreCase ? SpecialStringComparisons.IgnoreCase : SpecialStringComparisons.None));
                    if (firstSection != null)
                    {
                        firstSection.Properties.AddRange(Sections[i].Properties);
                        removedSections.Add(Sections[i]);
                    }
                }
                break;

            case IniDuplicateSectionNameBehavior.Duplicate:
                break;

            default:
                throw Throw.InvalidEnumArgument(nameof(behavior), behavior);
            }

            Sections.RemoveRange(removedSections);
        }
Пример #2
0
 /// <summary>
 /// Processes all duplicate sections within this collection according to the specified behavior.
 /// </summary>
 /// <param name="behavior">An <see cref="IniDuplicateSectionNameBehavior" /> object specifying how duplicates are processed.</param>
 public void ProcessDuplicates(IniDuplicateSectionNameBehavior behavior)
 {
     ProcessDuplicates(behavior, false);
 }