ConfigurationSectionGroup
using System.Runtime.Versioning;
namespace System.Configuration
{
public class ConfigurationSectionGroup
{
private MgmtConfigurationRecord _configRecord;
private ConfigurationSectionGroupCollection _configSectionGroups;
private ConfigurationSectionCollection _configSections;
private string _typeName;
internal bool Attached => _configRecord != null;
public bool IsDeclared { get; set; }
public bool IsDeclarationRequired { get; set; }
public string SectionGroupName { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public string Type {
get {
return _typeName;
}
set {
if (IsRoot)
throw new InvalidOperationException(System.SR.Config_root_section_group_cannot_be_edited);
string text = value;
if (string.IsNullOrEmpty(text))
text = null;
if (_configRecord != null) {
if (_configRecord.IsLocationConfig)
throw new InvalidOperationException(System.SR.Config_cannot_edit_configurationsectiongroup_in_location_config);
if (text != null) {
FactoryRecord factoryRecord = FindParentFactoryRecord(false);
if (factoryRecord != null && !factoryRecord.IsEquivalentType(_configRecord.Host, text))
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Config_tag_name_already_defined, SectionGroupName));
}
}
_typeName = text;
}
}
public ConfigurationSectionCollection Sections {
get {
if (_configSections == null) {
VerifyIsAttachedToConfigRecord();
_configSections = new ConfigurationSectionCollection(_configRecord, this);
}
return _configSections;
}
}
public ConfigurationSectionGroupCollection SectionGroups {
get {
if (_configSectionGroups == null) {
VerifyIsAttachedToConfigRecord();
_configSectionGroups = new ConfigurationSectionGroupCollection(_configRecord, this);
}
return _configSectionGroups;
}
}
internal bool IsRoot { get; set; }
internal void AttachToConfigurationRecord(MgmtConfigurationRecord configRecord, FactoryRecord factoryRecord)
{
_configRecord = configRecord;
SectionGroupName = factoryRecord.ConfigKey;
Name = factoryRecord.Name;
_typeName = factoryRecord.FactoryTypeName;
if (_typeName != null) {
FactoryRecord factoryRecord2 = null;
if (!configRecord.Parent.IsRootConfig)
factoryRecord2 = configRecord.Parent.FindFactoryRecord(factoryRecord.ConfigKey, true);
IsDeclarationRequired = (factoryRecord2?.FactoryTypeName == null);
IsDeclared = (configRecord.GetFactoryRecord(factoryRecord.ConfigKey, true) != null);
}
}
internal void RootAttachToConfigurationRecord(MgmtConfigurationRecord configRecord)
{
_configRecord = configRecord;
IsRoot = true;
}
internal void DetachFromConfigurationRecord()
{
_configSections?.DetachFromConfigurationRecord();
_configSectionGroups?.DetachFromConfigurationRecord();
_configRecord = null;
}
private FactoryRecord FindParentFactoryRecord(bool permitErrors)
{
FactoryRecord result = null;
if (_configRecord != null && !_configRecord.Parent.IsRootConfig)
result = _configRecord.Parent.FindFactoryRecord(SectionGroupName, permitErrors);
return result;
}
private void VerifyIsAttachedToConfigRecord()
{
if (_configRecord == null)
throw new InvalidOperationException(System.SR.Config_cannot_edit_configurationsectiongroup_when_not_attached);
}
public void ForceDeclaration()
{
ForceDeclaration(true);
}
public void ForceDeclaration(bool force)
{
if (IsRoot)
throw new InvalidOperationException(System.SR.Config_root_section_group_cannot_be_edited);
if (_configRecord != null && _configRecord.IsLocationConfig)
throw new InvalidOperationException(System.SR.Config_cannot_edit_configurationsectiongroup_in_location_config);
if (force || !IsDeclarationRequired)
IsDeclared = force;
}
protected internal virtual bool ShouldSerializeSectionGroupInTargetVersion(FrameworkName targetFramework)
{
return true;
}
}
}