GetSectionInstance() private method

private GetSectionInstance ( System.Configuration.SectionInfo config, bool createDefaultInstance ) : ConfigurationSection
config System.Configuration.SectionInfo
createDefaultInstance bool
return ConfigurationSection
Beispiel #1
0
 public ConfigurationSection this[string name]
 {
     get
     {
         var sec = BaseGet(name) as ConfigurationSection;
         if (sec == null)
         {
             var secData = _group.Sections[name] as SectionInfo;
             if (secData == null)
             {
                 return(null);
             }
             sec = _config.GetSectionInstance(secData, true);
             if (sec == null)
             {
                 return(null);
             }
             lock (LockObject)
             {
                 BaseSet(name, sec);
             }
         }
         return(sec);
     }
 }
Beispiel #2
0
        internal override void ResetModified(Configuration config)
        {
            ConfigurationSection section = config.GetSectionInstance(this, false);

            if (section != null)
            {
                section.ResetModified();
            }
        }
Beispiel #3
0
        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);
        }
Beispiel #4
0
        internal override bool HasValues(Configuration config, ConfigurationSaveMode mode)
        {
            var section = config.GetSectionInstance(this, false);

            if (section == null)
            {
                return(false);
            }

            var parent = config.Parent != null?config.Parent.GetSectionInstance(this, false) : null;

            return(section.HasValues(parent, mode));
        }
Beispiel #5
0
        public override void WriteData(Configuration config, XmlWriter writer, ConfigurationSaveMode mode)
        {
            string xml;

            ConfigurationSection section = config.GetSectionInstance(this, false);

            if (section != null)
            {
                ConfigurationSection parentSection = config.Parent != null?config.Parent.GetSectionInstance(this, false) : null;

                xml = section.SerializeSection(parentSection, Name, mode);

                string externalDataXml = section.ExternalDataXml;
                string filePath        = config.FilePath;

                if (!String.IsNullOrEmpty(filePath) && !String.IsNullOrEmpty(externalDataXml))
                {
                    string path = Path.Combine(Path.GetDirectoryName(filePath), section.SectionInformation.ConfigSource);
                    using (StreamWriter sw = new StreamWriter(path))
                    {
                        sw.Write(externalDataXml);
                    }
                }

                if (section.SectionInformation.IsProtected)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendFormat("<{0} configProtectionProvider=\"{1}\">\n",
                                    Name,
                                    section.SectionInformation.ProtectionProvider.Name);
                    sb.Append(config.ConfigHost.EncryptSection(xml,
                                                               section.SectionInformation.ProtectionProvider,
                                                               ProtectedConfiguration.Section));
                    sb.AppendFormat("</{0}>", Name);
                    xml = sb.ToString();
                }
            }
            else
            {
                xml = config.GetSectionXml(this);
            }

            if (xml != null)
            {
                writer.WriteRaw(xml);

                /*				XmlTextReader tr = new XmlTextReader (new StringReader (xml));
                 *                          writer.WriteNode (tr, true);
                 *                          tr.Close ();*/
            }
        }
Beispiel #6
0
 public ConfigurationSection this [string name]
 {
     get {
         ConfigurationSection sec = BaseGet(name) as ConfigurationSection;
         if (sec == null)
         {
             SectionInfo secData = group.Sections [name] as SectionInfo;
             if (secData == null)
             {
                 return(null);
             }
             sec = config.GetSectionInstance(secData, true);
             if (sec == null)
             {
                 return(null);
             }
             BaseSet(name, sec);
         }
         return(sec);
     }
 }
Beispiel #7
0
 public override bool HasDataContent(Configuration config)
 {
     return(config.GetSectionInstance(this, false) != null || config.GetSectionXml(this) != null);
 }
Beispiel #8
0
		public override bool HasDataContent (Configuration config)
		{
			return config.GetSectionInstance (this, false) != null || config.GetSectionXml (this) != null;
		}
Beispiel #9
0
		internal override void ResetModified (Configuration config)
		{
			ConfigurationSection section = config.GetSectionInstance (this, false);
			if (section != null)
				section.ResetModified ();
		}
Beispiel #10
0
		internal override bool HasValues (Configuration config, ConfigurationSaveMode mode)
		{
			var section = config.GetSectionInstance (this, false);
			if (section == null)
				return false;

			var parent = config.Parent != null ? config.Parent.GetSectionInstance (this, false) : null;
			return section.HasValues (parent, mode);
		}
Beispiel #11
0
		public override void WriteData (Configuration config, XmlWriter writer, ConfigurationSaveMode mode)
		{
			string xml;
			
			ConfigurationSection section = config.GetSectionInstance (this, false);
			if (section != null) {
				ConfigurationSection parentSection = config.Parent != null ? config.Parent.GetSectionInstance (this, false) : null;
				xml = section.SerializeSection (parentSection, Name, mode);

				string externalDataXml = section.ExternalDataXml;
				string filePath = config.FilePath;
				
				if (!String.IsNullOrEmpty (filePath) && !String.IsNullOrEmpty (externalDataXml)) {
					string path = Path.Combine (Path.GetDirectoryName (filePath), section.SectionInformation.ConfigSource);
					using (StreamWriter sw = new StreamWriter (path)) {
						sw.Write (externalDataXml);
					}
				}
				
				if (section.SectionInformation.IsProtected) {
					StringBuilder sb = new StringBuilder ();
					sb.AppendFormat ("<{0} configProtectionProvider=\"{1}\">\n",
							 Name,
							 section.SectionInformation.ProtectionProvider.Name);
					sb.Append (config.ConfigHost.EncryptSection (xml,
										     section.SectionInformation.ProtectionProvider,
										     ProtectedConfiguration.Section));
					sb.AppendFormat ("</{0}>", Name);
					xml = sb.ToString ();
				}
			}
			else {
				xml = config.GetSectionXml (this);
			}
			
			if (!string.IsNullOrEmpty (xml)) {
				writer.WriteRaw (xml);
/*				XmlTextReader tr = new XmlTextReader (new StringReader (xml));
				writer.WriteNode (tr, true);
				tr.Close ();*/
			}
		}