System.Configuration.MgmtConfigurationRecord.AddConfigurationSection C# (CSharp) Method

AddConfigurationSection() private method

private AddConfigurationSection ( string group, string name, ConfigurationSection configSection ) : void
group string
name string
configSection ConfigurationSection
return void
        internal void AddConfigurationSection(string group, string name, ConfigurationSection configSection) {

            // <configSections> is not permitted within a <location> tag.
            if (IsLocationConfig) {
                throw new InvalidOperationException(SR.GetString(SR.Config_add_configurationsection_in_location_config));
            }

            VerifySectionName(name, null, false);

            if (configSection == null) {
                throw new ArgumentNullException("configSection");
            }

            // Ensure the section is not already part of the configuration hierarchy.
            if (configSection.SectionInformation.Attached) {
                throw new InvalidOperationException(SR.GetString(SR.Config_add_configurationsection_already_added));
            }

            string configKey = BaseConfigurationRecord.CombineConfigKey(group, name);

            // Ensure the section is not already declared.
            FactoryRecord factoryRecord = FindFactoryRecord(configKey, true);
            if (factoryRecord != null) {
                throw new ArgumentException(SR.GetString(SR.Config_add_configurationsection_already_exists));
            }

            // Add the configSource if needed.
            if (!String.IsNullOrEmpty(configSection.SectionInformation.ConfigSource)) {
                ChangeConfigSource(configSection.SectionInformation, null, null, configSection.SectionInformation.ConfigSource);
            }

            // Add to list of all sections.
            if (_sectionFactories != null) {
                _sectionFactories.Add(configKey, new FactoryId(configKey, group, name));
            }

            // Get the type name.
            string typeName = configSection.SectionInformation.Type;
            if (typeName == null) {
                typeName = Host.GetConfigTypeName(configSection.GetType());
            }

            // Add a factory record for the section.
            factoryRecord = new FactoryRecord(configKey, 
                    group, 
                    name, 
                    typeName,
                    configSection.SectionInformation.AllowLocation, 
                    configSection.SectionInformation.AllowDefinition, 
                    configSection.SectionInformation.AllowExeDefinition, 
                    configSection.SectionInformation.RestartOnExternalChanges,
                    configSection.SectionInformation.RequirePermission,
                    _flags[IsTrusted],
                    false,  // isUndeclared
                    ConfigStreamInfo.StreamName,
                    -1);

            // Construct a factory for the section
            factoryRecord.Factory = TypeUtil.GetConstructorWithReflectionPermission(
                    configSection.GetType(), typeof(ConfigurationSection), true);

            factoryRecord.IsFactoryTrustedWithoutAptca = TypeUtil.IsTypeFromTrustedAssemblyWithoutAptca(configSection.GetType());

            EnsureFactories()[configKey] = factoryRecord;

            // Add a section record for the section.
            // Since we are adding a new definition, it cannot be locked.
            SectionRecord sectionRecord = EnsureSectionRecordUnsafe(configKey, false);
            sectionRecord.Result = configSection;
            sectionRecord.ResultRuntimeObject = configSection;

            // Undo any previous removals of the section.
            if (_removedSections != null) {
                _removedSections.Remove(configKey);
            }

            // Attach the section to the configuration record.
            configSection.SectionInformation.AttachToConfigurationRecord(this, factoryRecord, sectionRecord);

            //
            // If there is rawXml, set it now. Note this will override any other changes to the section
            // definition made after the call to SetXml.
            //
            string rawXml = configSection.SectionInformation.RawXml;
            if (!String.IsNullOrEmpty(rawXml)) {
                configSection.SectionInformation.RawXml = null;
                configSection.SectionInformation.SetRawXml(rawXml);
            }
        }