ConfigurationSectionCollection.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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
  30. using System;
  31. using System.Collections;
  32. using System.Collections.Specialized;
  33. using System.Runtime.Serialization;
  34. namespace System.Configuration
  35. {
  36. [Serializable]
  37. public sealed class ConfigurationSectionCollection : NameObjectCollectionBase
  38. {
  39. SectionGroupInfo group;
  40. Configuration config;
  41. internal ConfigurationSectionCollection (Configuration config, SectionGroupInfo group)
  42. : base (StringComparer.Ordinal)
  43. {
  44. this.config = config;
  45. this.group = group;
  46. }
  47. public override NameObjectCollectionBase.KeysCollection Keys
  48. {
  49. get { return group.Sections.Keys; }
  50. }
  51. public override int Count
  52. {
  53. get { return group.Sections.Count; }
  54. }
  55. public ConfigurationSection this [string name]
  56. {
  57. get {
  58. ConfigurationSection sec = BaseGet (name) as ConfigurationSection;
  59. if (sec == null) {
  60. SectionInfo secData = group.Sections [name] as SectionInfo;
  61. if (secData == null) return null;
  62. sec = config.GetSectionInstance (secData, true);
  63. if (sec == null) return null;
  64. BaseSet (name, sec);
  65. }
  66. return sec;
  67. }
  68. }
  69. public ConfigurationSection this [int index]
  70. {
  71. get { return this [GetKey (index)]; }
  72. }
  73. public void Add (string name, ConfigurationSection section)
  74. {
  75. config.CreateSection (group, name, section);
  76. }
  77. public void Clear ()
  78. {
  79. if (group.Sections != null) {
  80. foreach (ConfigInfo data in group.Sections)
  81. config.RemoveConfigInfo (data);
  82. }
  83. }
  84. public void CopyTo (ConfigurationSection [] array, int index)
  85. {
  86. for (int n=0; n<group.Sections.Count; n++)
  87. array [n + index] = this [n];
  88. }
  89. public ConfigurationSection Get (int index)
  90. {
  91. return this [index];
  92. }
  93. public ConfigurationSection Get (string name)
  94. {
  95. return this [name];
  96. }
  97. public override IEnumerator GetEnumerator()
  98. {
  99. return group.Sections.AllKeys.GetEnumerator ();
  100. }
  101. public string GetKey (int index)
  102. {
  103. return group.Sections.GetKey (index);
  104. }
  105. public void Remove (string name)
  106. {
  107. SectionInfo secData = group.Sections [name] as SectionInfo;
  108. if (secData != null)
  109. config.RemoveConfigInfo (secData);
  110. }
  111. public void RemoveAt (int index)
  112. {
  113. SectionInfo secData = group.Sections [index] as SectionInfo;
  114. config.RemoveConfigInfo (secData);
  115. }
  116. [MonoTODO]
  117. public override void GetObjectData (SerializationInfo info, StreamingContext context)
  118. {
  119. throw new NotImplementedException ();
  120. }
  121. }
  122. }
  123. #endif