private object CreateSectionImpl(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, object parentConfig, ConfigXmlReader reader) { if (this._sectionCtor != null) { ConfigurationSection section = (ConfigurationSection)System.Configuration.TypeUtil.InvokeCtorWithReflectionPermission(this._sectionCtor); section.SectionInformation.SetRuntimeConfigurationInformation(configRecord, factoryRecord, sectionRecord); section.CallInit(); ConfigurationSection parentElement = (ConfigurationSection)parentConfig; section.Reset(parentElement); if (reader != null) { section.DeserializeSection(reader); } ConfigurationErrorsException errors = section.GetErrors(); if (errors != null) { throw errors; } section.SetReadOnly(); section.ResetModified(); return(section); } if (reader != null) { XmlNode xmlNode = ErrorInfoXmlDocument.CreateSectionXmlNode(reader); CheckForLockAttributes(factoryRecord.ConfigKey, xmlNode); object configContext = configRecord.Host.CreateDeprecatedConfigContext(configRecord.ConfigPath); return(this._sectionHandler.Create(parentConfig, configContext, xmlNode)); } return(null); }
internal object CreateSection(bool inputIsTrusted, RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, object parentConfig, ConfigXmlReader reader) { object config; if (_sectionCtor != null) { ConfigurationSection configSection = (ConfigurationSection)_sectionCtor.Invoke(null); configSection.SectionInformation.SetRuntimeConfigurationInformation(configRecord, factoryRecord, sectionRecord); configSection.CallInit(); ConfigurationSection parentSection = (ConfigurationSection)parentConfig; configSection.Reset(parentSection); if (reader != null) { configSection.DeserializeSection(reader); } // throw if there are any cached errors ConfigurationErrorsException errors = configSection.GetErrors(); if (errors != null) { throw errors; } // don't allow changes to sections at runtime configSection.SetReadOnly(); // reset the modified bit configSection.ResetModified(); config = configSection; } else { if (reader != null) { XmlNode xmlNode = ErrorInfoXmlDocument.CreateSectionXmlNode(reader); CheckForLockAttributes(factoryRecord.ConfigKey, xmlNode); // In v1, our old section handler expects a context that contains the virtualPath from the configPath object configContext = configRecord.Host.CreateDeprecatedConfigContext(configRecord.ConfigPath); config = _sectionHandler.Create(parentConfig, configContext, xmlNode); } else { config = null; } } return(config); }
internal ConfigurationSection GetSectionInstance(SectionInfo config, bool createDefaultInstance) { object data = elementData [config]; ConfigurationSection sec = data as ConfigurationSection; if (sec != null || !createDefaultInstance) { return(sec); } object secObj = config.CreateInstance(); sec = secObj as ConfigurationSection; if (sec == null) { DefaultSection ds = new DefaultSection(); ds.SectionHandler = secObj as IConfigurationSectionHandler; sec = ds; } sec.Configuration = this; ConfigurationSection parentSection = null; if (parent != null) { parentSection = parent.GetSectionInstance(config, true); sec.SectionInformation.SetParentSection(parentSection); } sec.SectionInformation.ConfigFilePath = FilePath; sec.ConfigContext = system.Host.CreateDeprecatedConfigContext(configPath); string xml = data as string; sec.RawXml = xml; sec.Reset(parentSection); if (xml != null) { XmlTextReader r = new ConfigXmlTextReader(new StringReader(xml), FilePath); sec.DeserializeSection(r); r.Close(); if (!String.IsNullOrEmpty(sec.SectionInformation.ConfigSource) && !String.IsNullOrEmpty(FilePath)) { sec.DeserializeConfigSource(Path.GetDirectoryName(FilePath)); } } elementData [config] = sec; return(sec); }
// // Update the section with the XML provided. // // This method will throw out any changes made to the section up to this point. // // If xmlElement is null or empty, it is equivalent to calling RevertToParent // internal void SetRawXml(ConfigurationSection configSection, string xmlElement) { // Null or empty is equivalent to RevertToParent(). if (string.IsNullOrEmpty(xmlElement)) { RevertToParent(configSection); return; } ValidateSectionXml(xmlElement, configSection.SectionInformation.Name); // Reset the ConfigurationSection with the XML. ConfigurationSection parentConfigSection = FindImmediateParentSection(configSection); ConfigXmlReader reader = new ConfigXmlReader(xmlElement, null, 0); // Store the raw XML. configSection.SectionInformation.RawXml = xmlElement; // Update the section with the xml try { try { bool wasPresent = configSection.ElementPresent; PropertySourceInfo saveInfo = configSection.ElementInformation.PropertyInfoInternal(); configSection.Reset(parentConfigSection); configSection.DeserializeSection(reader); configSection.ResetModified(); configSection.ElementPresent = wasPresent; configSection.ElementInformation.ChangeSourceAndLineNumber(saveInfo); } catch { configSection.SectionInformation.RawXml = null; throw; } } catch (Exception e) { throw new ConfigurationErrorsException(SR.GetString(SR.Config_exception_in_config_section_handler, configSection.SectionInformation.SectionName), e, null, 0); } // Ignore previous attempts to remove the section. configSection.SectionInformation.Removed = false; }
// // Revert the ConfigurationSection to the value of its parent. // internal void RevertToParent(ConfigurationSection configSection) { // Remove any RawXml set by ConfigurationSection.SetRawXml configSection.SectionInformation.RawXml = null; try { // Reset to parent value ConfigurationSection parentConfigSection = FindImmediateParentSection(configSection); configSection.Reset(parentConfigSection); // Consider it to be unmodified configSection.ResetModified(); } catch (Exception e) { throw new ConfigurationErrorsException(SR.GetString(SR.Config_exception_in_config_section_handler, configSection.SectionInformation.SectionName), e, ConfigStreamInfo.StreamName, 0); } // Record that the section is to be removed. configSection.SectionInformation.Removed = true; }
internal void SetRawXml(ConfigurationSection configSection, string xmlElement) { if (string.IsNullOrEmpty(xmlElement)) { this.RevertToParent(configSection); } else { this.ValidateSectionXml(xmlElement, configSection.SectionInformation.Name); ConfigurationSection parentElement = this.FindImmediateParentSection(configSection); ConfigXmlReader reader = new ConfigXmlReader(xmlElement, null, 0); configSection.SectionInformation.RawXml = xmlElement; try { try { bool elementPresent = configSection.ElementPresent; PropertySourceInfo sourceInformation = configSection.ElementInformation.PropertyInfoInternal(); configSection.Reset(parentElement); configSection.DeserializeSection(reader); configSection.ResetModified(); configSection.ElementPresent = elementPresent; configSection.ElementInformation.ChangeSourceAndLineNumber(sourceInformation); } catch { configSection.SectionInformation.RawXml = null; throw; } } catch (Exception exception) { throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Config_exception_in_config_section_handler", new object[] { configSection.SectionInformation.SectionName }), exception, null, 0); } configSection.SectionInformation.Removed = false; } }
internal void RevertToParent(ConfigurationSection configSection) { configSection.SectionInformation.RawXml = null; try { ConfigurationSection parentElement = this.FindImmediateParentSection(configSection); configSection.Reset(parentElement); configSection.ResetModified(); } catch (Exception exception) { throw new ConfigurationErrorsException(System.Configuration.SR.GetString("Config_exception_in_config_section_handler", new object[] { configSection.SectionInformation.SectionName }), exception, base.ConfigStreamInfo.StreamName, 0); } configSection.SectionInformation.Removed = true; }