SectionInformation
using System.Configuration.Internal;
namespace System.Configuration
{
public sealed class SectionInformation
{
private const int FlagAttached = 1;
private const int FlagDeclared = 2;
private const int FlagDeclarationRequired = 4;
private const int FlagAllowLocation = 8;
private const int FlagRestartOnExternalChanges = 16;
private const int FlagRequirePermission = 32;
private const int FlagLocationLocked = 64;
private const int FlagChildrenLocked = 128;
private const int FlagInheritInChildApps = 256;
private const int FlagIsParentSection = 512;
private const int FlagRemoved = 1024;
private const int FlagProtectionProviderDetermined = 2048;
private const int FlagForceSave = 4096;
private const int FlagIsUndeclared = 8192;
private const int FlagChildrenLockWithoutFileInput = 16384;
private const int FlagAllowExeDefinitionModified = 65536;
private const int FlagAllowDefinitionModified = 131072;
private const int FlagConfigSourceModified = 262144;
private const int FlagProtectionProviderModified = 524288;
private const int FlagOverrideModeDefaultModified = 1048576;
private const int FlagOverrideModeModified = 2097152;
private readonly ConfigurationSection _configurationSection;
private ConfigurationAllowDefinition _allowDefinition;
private ConfigurationAllowExeDefinition _allowExeDefinition;
private MgmtConfigurationRecord _configRecord;
private string _configSource;
private SafeBitVector32 _flags;
private SimpleBitVector32 _modifiedFlags;
private OverrideModeSetting _overrideMode;
private OverrideModeSetting _overrideModeDefault;
private ProtectedConfigurationProvider _protectionProvider;
private string _typeName;
private bool IsRuntime {
get {
if (_flags[1])
return _configRecord == null;
return false;
}
}
internal bool Attached => _flags[1];
internal string ConfigKey { get; set; }
internal bool Removed {
get {
return _flags[1024];
}
set {
_flags[1024] = value;
}
}
public string SectionName => ConfigKey;
public string Name { get; set; }
public ConfigurationAllowDefinition AllowDefinition {
get {
return _allowDefinition;
}
set {
VerifyIsEditable();
VerifyIsEditableFactory();
FactoryRecord factoryRecord = FindParentFactoryRecord(false);
if (factoryRecord != null && factoryRecord.AllowDefinition != value)
throw new ConfigurationErrorsException(string.Format(System.SR.Config_tag_name_already_defined, ConfigKey));
_allowDefinition = value;
_modifiedFlags[131072] = true;
}
}
internal bool AllowDefinitionModified => _modifiedFlags[131072];
public ConfigurationAllowExeDefinition AllowExeDefinition {
get {
return _allowExeDefinition;
}
set {
VerifyIsEditable();
VerifyIsEditableFactory();
FactoryRecord factoryRecord = FindParentFactoryRecord(false);
if (factoryRecord != null && factoryRecord.AllowExeDefinition != value)
throw new ConfigurationErrorsException(string.Format(System.SR.Config_tag_name_already_defined, ConfigKey));
_allowExeDefinition = value;
_modifiedFlags[65536] = true;
}
}
internal bool AllowExeDefinitionModified => _modifiedFlags[65536];
public OverrideMode OverrideModeDefault {
get {
return _overrideModeDefault.OverrideMode;
}
set {
VerifyIsEditable();
VerifyIsEditableFactory();
FactoryRecord factoryRecord = FindParentFactoryRecord(false);
if (factoryRecord != null && factoryRecord.OverrideModeDefault.OverrideMode != value)
throw new ConfigurationErrorsException(string.Format(System.SR.Config_tag_name_already_defined, ConfigKey));
if (value == OverrideMode.Inherit)
value = OverrideMode.Allow;
_overrideModeDefault.OverrideMode = value;
_modifiedFlags[1048576] = true;
}
}
internal OverrideModeSetting OverrideModeDefaultSetting => _overrideModeDefault;
internal bool OverrideModeDefaultModified => _modifiedFlags[1048576];
public bool AllowLocation {
get {
return _flags[8];
}
set {
VerifyIsEditable();
VerifyIsEditableFactory();
FactoryRecord factoryRecord = FindParentFactoryRecord(false);
if (factoryRecord != null && factoryRecord.AllowLocation != value)
throw new ConfigurationErrorsException(string.Format(System.SR.Config_tag_name_already_defined, ConfigKey));
_flags[8] = value;
_modifiedFlags[8] = true;
}
}
internal bool AllowLocationModified => _modifiedFlags[8];
public bool AllowOverride {
get {
return _overrideMode.AllowOverride;
}
set {
VerifyIsEditable();
VerifySupportsLocation();
_overrideMode.AllowOverride = value;
_modifiedFlags[2097152] = true;
}
}
public OverrideMode OverrideMode {
get {
return _overrideMode.OverrideMode;
}
set {
VerifyIsEditable();
VerifySupportsLocation();
_overrideMode.OverrideMode = value;
_modifiedFlags[2097152] = true;
switch (value) {
case OverrideMode.Inherit:
_flags[128] = _flags[16384];
break;
case OverrideMode.Allow:
_flags[128] = false;
break;
case OverrideMode.Deny:
_flags[128] = true;
break;
}
}
}
public OverrideMode OverrideModeEffective {
get {
if (!_flags[128])
return OverrideMode.Allow;
return OverrideMode.Deny;
}
}
internal OverrideModeSetting OverrideModeSetting => _overrideMode;
internal bool LocationAttributesAreDefault {
get {
if (_overrideMode.IsDefaultForLocationTag)
return _flags[256];
return false;
}
}
public string ConfigSource {
get {
return _configSource ?? string.Empty;
}
set {
VerifyIsEditable();
string text = (!string.IsNullOrEmpty(value)) ? BaseConfigurationRecord.NormalizeConfigSource(value, null) : null;
if (!(text == _configSource)) {
_configRecord?.ChangeConfigSource(this, _configSource, ConfigSourceStreamName, text);
_configSource = text;
_modifiedFlags[262144] = true;
}
}
}
internal bool ConfigSourceModified => _modifiedFlags[262144];
internal string ConfigSourceStreamName { get; set; }
public bool InheritInChildApplications {
get {
return _flags[256];
}
set {
VerifyIsEditable();
VerifySupportsLocation();
_flags[256] = value;
}
}
public bool IsDeclared {
get {
VerifyNotParentSection();
return _flags[2];
}
}
public bool IsDeclarationRequired {
get {
VerifyNotParentSection();
return _flags[4];
}
}
private bool IsDefinitionAllowed {
get {
if (_configRecord != null)
return _configRecord.IsDefinitionAllowed(_allowDefinition, _allowExeDefinition);
return true;
}
}
public bool IsLocked {
get {
if (!_flags[64] && IsDefinitionAllowed)
return _configurationSection.ElementInformation.IsLocked;
return true;
}
}
public bool IsProtected => ProtectionProvider != null;
public ProtectedConfigurationProvider ProtectionProvider {
get {
if (!_flags[2048] && _configRecord != null) {
_protectionProvider = _configRecord.GetProtectionProviderFromName(ProtectionProviderName, false);
_flags[2048] = true;
}
return _protectionProvider;
}
}
internal string ProtectionProviderName { get; set; }
public bool RestartOnExternalChanges {
get {
return _flags[16];
}
set {
VerifyIsEditable();
VerifyIsEditableFactory();
FactoryRecord factoryRecord = FindParentFactoryRecord(false);
if (factoryRecord != null && factoryRecord.RestartOnExternalChanges != value)
throw new ConfigurationErrorsException(string.Format(System.SR.Config_tag_name_already_defined, ConfigKey));
_flags[16] = value;
_modifiedFlags[16] = true;
}
}
internal bool RestartOnExternalChangesModified => _modifiedFlags[16];
public bool RequirePermission {
get {
return _flags[32];
}
set {
VerifyIsEditable();
VerifyIsEditableFactory();
FactoryRecord factoryRecord = FindParentFactoryRecord(false);
if (factoryRecord != null && factoryRecord.RequirePermission != value)
throw new ConfigurationErrorsException(string.Format(System.SR.Config_tag_name_already_defined, ConfigKey));
_flags[32] = value;
_modifiedFlags[32] = true;
}
}
internal bool RequirePermissionModified => _modifiedFlags[32];
public string Type {
get {
return _typeName;
}
set {
if (string.IsNullOrEmpty(value))
throw ExceptionUtil.PropertyNullOrEmpty("Type");
VerifyIsEditable();
VerifyIsEditableFactory();
FactoryRecord factoryRecord = FindParentFactoryRecord(false);
if (factoryRecord != null) {
IInternalConfigHost host = null;
if (_configRecord != null)
host = _configRecord.Host;
if (!factoryRecord.IsEquivalentType(host, value))
throw new ConfigurationErrorsException(string.Format(System.SR.Config_tag_name_already_defined, ConfigKey));
}
_typeName = value;
}
}
internal string RawXml { get; set; }
public bool ForceSave {
get {
return _flags[4096];
}
set {
VerifyIsEditable();
_flags[4096] = value;
}
}
internal SectionInformation(ConfigurationSection associatedConfigurationSection)
{
ConfigKey = string.Empty;
Name = string.Empty;
_configurationSection = associatedConfigurationSection;
_allowDefinition = ConfigurationAllowDefinition.Everywhere;
_allowExeDefinition = ConfigurationAllowExeDefinition.MachineToApplication;
_overrideModeDefault = OverrideModeSetting.s_sectionDefault;
_overrideMode = OverrideModeSetting.s_locationDefault;
_flags[8] = true;
_flags[16] = true;
_flags[32] = true;
_flags[256] = true;
_flags[4096] = false;
_modifiedFlags = default(SimpleBitVector32);
}
internal void ResetModifiedFlags()
{
_modifiedFlags = default(SimpleBitVector32);
}
internal bool IsModifiedFlags()
{
return _modifiedFlags.Data != 0;
}
internal void AttachToConfigurationRecord(MgmtConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord)
{
SetRuntimeConfigurationInformation(configRecord, factoryRecord, sectionRecord);
_configRecord = configRecord;
}
internal void SetRuntimeConfigurationInformation(BaseConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord)
{
_flags[1] = true;
ConfigKey = factoryRecord.ConfigKey;
Name = factoryRecord.Name;
_typeName = factoryRecord.FactoryTypeName;
_allowDefinition = factoryRecord.AllowDefinition;
_allowExeDefinition = factoryRecord.AllowExeDefinition;
_flags[8] = factoryRecord.AllowLocation;
_flags[16] = factoryRecord.RestartOnExternalChanges;
_flags[32] = factoryRecord.RequirePermission;
_overrideModeDefault = factoryRecord.OverrideModeDefault;
if (factoryRecord.IsUndeclared) {
_flags[8192] = true;
_flags[2] = false;
_flags[4] = false;
} else {
_flags[8192] = false;
_flags[2] = (configRecord.GetFactoryRecord(factoryRecord.ConfigKey, false) != null);
_flags[4] = configRecord.IsRootDeclaration(factoryRecord.ConfigKey, false);
}
_flags[64] = sectionRecord.Locked;
_flags[128] = sectionRecord.LockChildren;
_flags[16384] = sectionRecord.LockChildrenWithoutFileInput;
if (sectionRecord.HasFileInput) {
SectionInput fileInput = sectionRecord.FileInput;
_flags[2048] = fileInput.IsProtectionProviderDetermined;
_protectionProvider = fileInput.ProtectionProvider;
SectionXmlInfo sectionXmlInfo = fileInput.SectionXmlInfo;
_configSource = sectionXmlInfo.ConfigSource;
ConfigSourceStreamName = sectionXmlInfo.ConfigSourceStreamName;
_overrideMode = sectionXmlInfo.OverrideModeSetting;
_flags[256] = !sectionXmlInfo.SkipInChildApps;
ProtectionProviderName = sectionXmlInfo.ProtectionProviderName;
} else {
_flags[2048] = false;
_protectionProvider = null;
}
_configurationSection.AssociateContext(configRecord);
}
internal void DetachFromConfigurationRecord()
{
RevertToParent();
_flags[1] = false;
_configRecord = null;
}
private void VerifyDesigntime()
{
if (IsRuntime)
throw new InvalidOperationException(System.SR.Config_operation_not_runtime);
}
private void VerifyIsAttachedToConfigRecord()
{
if (_configRecord == null)
throw new InvalidOperationException(System.SR.Config_cannot_edit_configurationsection_when_not_attached);
}
internal void VerifyIsEditable()
{
VerifyDesigntime();
if (IsLocked)
throw new InvalidOperationException(System.SR.Config_cannot_edit_configurationsection_when_locked);
if (_flags[512])
throw new InvalidOperationException(System.SR.Config_cannot_edit_configurationsection_parentsection);
if (!_flags[8] && _configRecord != null && _configRecord.IsLocationConfig)
throw new InvalidOperationException(System.SR.Config_cannot_edit_configurationsection_when_location_locked);
}
private void VerifyNotParentSection()
{
if (_flags[512])
throw new InvalidOperationException(System.SR.Config_configsection_parentnotvalid);
}
private void VerifySupportsLocation()
{
if (_configRecord != null && !_configRecord.RecordSupportsLocation)
throw new InvalidOperationException(System.SR.Config_cannot_edit_locationattriubtes);
}
internal void VerifyIsEditableFactory()
{
if (_configRecord != null && _configRecord.IsLocationConfig)
throw new InvalidOperationException(System.SR.Config_cannot_edit_configurationsection_in_location_config);
if (BaseConfigurationRecord.IsImplicitSection(ConfigKey))
throw new InvalidOperationException(System.SR.Config_cannot_edit_configurationsection_when_it_is_implicit);
if (_flags[8192])
throw new InvalidOperationException(System.SR.Config_cannot_edit_configurationsection_when_it_is_undeclared);
}
private FactoryRecord FindParentFactoryRecord(bool permitErrors)
{
FactoryRecord result = null;
if (_configRecord != null && !_configRecord.Parent.IsRootConfig)
result = _configRecord.Parent.FindFactoryRecord(ConfigKey, permitErrors);
return result;
}
public void ForceDeclaration()
{
ForceDeclaration(true);
}
public void ForceDeclaration(bool force)
{
VerifyIsEditable();
if (force || !_flags[4]) {
if (force && BaseConfigurationRecord.IsImplicitSection(SectionName))
throw new ConfigurationErrorsException(System.SR.Format(System.SR.Cannot_declare_or_remove_implicit_section, SectionName));
if (force && _flags[8192])
throw new ConfigurationErrorsException(System.SR.Config_cannot_edit_configurationsection_when_it_is_undeclared);
_flags[2] = force;
}
}
public void ProtectSection(string protectionProvider)
{
VerifyIsEditable();
if (!AllowLocation || ConfigKey == "configProtectedData")
throw new InvalidOperationException(System.SR.Config_not_allowed_to_encrypt_this_section);
if (_configRecord == null)
throw new InvalidOperationException(System.SR.Must_add_to_config_before_protecting_it);
if (string.IsNullOrEmpty(protectionProvider))
protectionProvider = _configRecord.DefaultProviderName;
ProtectedConfigurationProvider protectionProviderFromName = _configRecord.GetProtectionProviderFromName(protectionProvider, true);
ProtectionProviderName = protectionProvider;
_protectionProvider = protectionProviderFromName;
_flags[2048] = true;
_modifiedFlags[524288] = true;
}
public void UnprotectSection()
{
VerifyIsEditable();
_protectionProvider = null;
ProtectionProviderName = null;
_flags[2048] = true;
_modifiedFlags[524288] = true;
}
public ConfigurationSection GetParentSection()
{
VerifyDesigntime();
if (_flags[512])
throw new InvalidOperationException(System.SR.Config_getparentconfigurationsection_first_instance);
ConfigurationSection configurationSection = null;
if (_configRecord != null) {
configurationSection = _configRecord.FindAndCloneImmediateParentSection(_configurationSection);
if (configurationSection != null) {
configurationSection.SectionInformation._flags[512] = true;
configurationSection.SetReadOnly();
}
}
return configurationSection;
}
public string GetRawXml()
{
VerifyDesigntime();
VerifyNotParentSection();
object rawXml = RawXml;
if (rawXml == null) {
MgmtConfigurationRecord configRecord = _configRecord;
if (configRecord == null)
return null;
rawXml = configRecord.GetRawXml(ConfigKey);
}
return (string)rawXml;
}
public void SetRawXml(string rawXml)
{
VerifyIsEditable();
if (_configRecord != null)
_configRecord.SetRawXml(_configurationSection, rawXml);
else
RawXml = (string.IsNullOrEmpty(rawXml) ? null : rawXml);
}
public void RevertToParent()
{
VerifyIsEditable();
VerifyIsAttachedToConfigRecord();
_configRecord.RevertToParent(_configurationSection);
}
}
}