internal void AddOrUpdate(SettingsFile settingsFile, string sectionName, SettingItem item) { if (string.IsNullOrEmpty(sectionName)) { throw new ArgumentException(Resources.Argument_Cannot_Be_Null_Or_Empty, nameof(sectionName)); } if (item == null) { throw new ArgumentNullException(nameof(item)); } var currentSettings = Priority.Last(f => f.Equals(settingsFile)); if (settingsFile.IsMachineWide || (currentSettings?.IsMachineWide ?? false)) { throw new InvalidOperationException(Resources.CannotUpdateMachineWide); } if (currentSettings == null) { Priority.First().SetNextFile(settingsFile); } // If it is an update this will take care of it and modify the underlaying object, which is also referenced by _computedSections. settingsFile.AddOrUpdate(sectionName, item); // AddOrUpdate should have created this section, therefore this should always exist. settingsFile.TryGetSection(sectionName, out var settingFileSection); // If it is an add we have to manually add it to the _computedSections. var computedSectionExists = _computedSections.TryGetValue(sectionName, out var section); if (computedSectionExists && !section.Items.Contains(item)) { var existingItem = settingFileSection.Items.First(i => i.Equals(item)); section.Add(existingItem); } else if (!computedSectionExists) { _computedSections.Add(sectionName, new VirtualSettingSection(settingFileSection)); } }