ConfigurationSectionCollection.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // System.Configuration.ConfigurationSectionCollection.cs
  3. //
  4. // Authors:
  5. // Duncan Mak ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  28. //
  29. #if NET_2_0 && XML_DEP
  30. using System;
  31. using System.Collections;
  32. using System.Collections.Specialized;
  33. namespace System.Configuration
  34. {
  35. public sealed class ConfigurationSectionCollection : NameObjectCollectionBase
  36. {
  37. SectionGroupInfo group;
  38. Configuration config;
  39. internal ConfigurationSectionCollection (Configuration config, SectionGroupInfo group)
  40. {
  41. this.config = config;
  42. this.group = group;
  43. }
  44. public ICollection AllKeys
  45. {
  46. get { return group.Sections.AllKeys; }
  47. }
  48. public override int Count
  49. {
  50. get { return group.Sections.Count; }
  51. }
  52. public ConfigurationSection this [string name]
  53. {
  54. get {
  55. ConfigurationSection sec = BaseGet (name) as ConfigurationSection;
  56. if (sec == null) {
  57. SectionInfo secData = group.Sections [name] as SectionInfo;
  58. if (secData == null) return null;
  59. sec = config.GetSectionInstance (secData, true);
  60. if (sec == null) return null;
  61. BaseSet (name, sec);
  62. }
  63. return sec;
  64. }
  65. }
  66. public ConfigurationSection this [int index]
  67. {
  68. get { return this [GetKey (index)]; }
  69. }
  70. public void Add (string name, ConfigurationSection section)
  71. {
  72. config.CreateSection (group, name, section);
  73. }
  74. public void Clear ()
  75. {
  76. if (group.Sections != null) {
  77. foreach (ConfigInfo data in group.Sections)
  78. config.RemoveConfigInfo (data);
  79. }
  80. }
  81. public void CopyTo (ConfigurationSection [] array, int index)
  82. {
  83. for (int n=0; n<group.Sections.Count; n++)
  84. array [n + index] = this [n];
  85. }
  86. public ConfigurationSection Get (int index)
  87. {
  88. return this [index];
  89. }
  90. public ConfigurationSection Get (string name)
  91. {
  92. return this [name];
  93. }
  94. public override IEnumerator GetEnumerator()
  95. {
  96. return group.Sections.AllKeys.GetEnumerator ();
  97. }
  98. public string GetKey (int index)
  99. {
  100. return group.Sections.GetKey (index);
  101. }
  102. public void Remove (string name)
  103. {
  104. SectionInfo secData = group.Sections [name] as SectionInfo;
  105. if (secData != null)
  106. config.RemoveConfigInfo (secData);
  107. }
  108. public void RemoveAt (int index)
  109. {
  110. SectionInfo secData = group.Sections [index] as SectionInfo;
  111. config.RemoveConfigInfo (secData);
  112. }
  113. }
  114. }
  115. #endif