2 // ServiceModelExtensionCollectionElement.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2006 Novell, Inc. http://www.novell.com
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Configuration;
34 namespace System.ServiceModel.Configuration
36 public abstract class ServiceModelExtensionCollectionElement<TServiceModelExtensionElement> : ConfigurationElement,
37 ICollection<TServiceModelExtensionElement>,
38 IEnumerable<TServiceModelExtensionElement>,
40 where TServiceModelExtensionElement : ServiceModelExtensionElement
42 internal ServiceModelExtensionCollectionElement ()
46 ConfigurationPropertyCollection properties;
48 KeyedByTypeCollection<TServiceModelExtensionElement> _list = new KeyedByTypeCollection<TServiceModelExtensionElement> ();
51 public virtual void Add (TServiceModelExtensionElement element)
57 public virtual bool CanAdd (TServiceModelExtensionElement element) {
58 throw new NotImplementedException ();
67 public bool Contains (TServiceModelExtensionElement element)
69 return _list.Contains (element);
72 public bool ContainsKey (string elementName)
74 throw new NotImplementedException ();
77 public bool ContainsKey (Type elementType)
79 return _list.Contains (elementType);
82 public void CopyTo (TServiceModelExtensionElement[] elements,
85 throw new NotImplementedException ();
88 public IEnumerator<TServiceModelExtensionElement> GetEnumerator () {
89 for (int i = 0; i < Count; i++)
90 yield return this [i];
93 public bool Remove (TServiceModelExtensionElement element)
96 return _list.Remove (element.GetType ());
99 IEnumerator IEnumerable.GetEnumerator ()
101 return GetEnumerator ();
104 bool ICollection<TServiceModelExtensionElement>.IsReadOnly {
105 get { throw new NotImplementedException (); }
108 public TServiceModelExtensionElement this [int index] {
110 return _list [index];
114 public TServiceModelExtensionElement this [Type extensionType] {
116 if (_list.Contains (extensionType))
117 return _list [extensionType];
122 protected override void DeserializeElement (XmlReader reader, bool serializeCollectionKey)
124 base.DeserializeElement (reader, serializeCollectionKey);
127 protected override bool OnDeserializeUnrecognizedElement (string elementName, XmlReader reader) {
128 TServiceModelExtensionElement ext= DeserializeExtensionElement (elementName, reader);
135 internal virtual TServiceModelExtensionElement DeserializeExtensionElement (string elementName, XmlReader reader) {
140 get { return _list.Count; }
143 protected override void Reset (ConfigurationElement parentElement)
145 base.Reset (parentElement);
146 var parent = (ServiceModelExtensionCollectionElement<TServiceModelExtensionElement>) parentElement;
149 foreach (var item in parent)
151 #else // FIXME: no way to call Reset() on item (hence disabled now)
152 // It is based on ConfigurationElementCollection.Reset()
153 for (int n=0; n<parent.Count; n++)
155 var parentItem = parent [n];
156 // ConfigurationElement item = CreateNewElementInternal (null);
157 var item = (TServiceModelExtensionElement) Activator.CreateInstance (parentItem.GetType ()); // FIXME: there is no assureance on default constructor existence.
158 item.Reset (parentItem);
164 protected override bool IsModified ()
169 protected override void ResetModified ()
174 protected void SetIsModified ()
179 protected override ConfigurationPropertyCollection Properties {
181 if (properties == null) {
182 properties = base.Properties;