Reverted accidental checkin
[castle.git] / Core / Castle.Core / ReflectionBasedDictionaryAdapter.cs
blob9738db993bbdab944e6aaa0e554003943e84fa9e
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.Core
17 using System;
18 using System.Collections;
19 using System.Reflection;
20 using System.Collections.Generic;
22 /// <summary>
23 /// Pendent
24 /// </summary>
25 public class ReflectionBasedDictionaryAdapter : IDictionary
27 private readonly Dictionary<string,object> properties = new Dictionary<string, object>(StringComparer.InvariantCultureIgnoreCase);
29 /// <summary>
30 /// Initializes a new instance of the <see cref="ReflectionBasedDictionaryAdapter"/> class.
31 /// </summary>
32 /// <param name="target">The target.</param>
33 public ReflectionBasedDictionaryAdapter(object target)
35 if (target == null)
37 throw new ArgumentNullException("target");
40 Type targetType = target.GetType();
42 foreach(PropertyInfo property in targetType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
44 object value = property.GetValue(target, null);
46 properties[property.Name] = value;
50 /// <summary>
51 /// Determines whether the <see cref="T:System.Collections.IDictionary"/> object contains an element with the specified key.
52 /// </summary>
53 /// <param name="key">The key to locate in the <see cref="T:System.Collections.IDictionary"/> object.</param>
54 /// <returns>
55 /// true if the <see cref="T:System.Collections.IDictionary"/> contains an element with the key; otherwise, false.
56 /// </returns>
57 /// <exception cref="T:System.ArgumentNullException">
58 /// <paramref name="key"/> is null. </exception>
59 public bool Contains(object key)
61 return properties.ContainsKey(key.ToString());
64 /// <summary>
65 /// Gets or sets the <see cref="System.Object"/> with the specified key.
66 /// </summary>
67 /// <value></value>
68 public object this[object key]
70 get
72 object value;
73 properties.TryGetValue(key.ToString(), out value);
74 return value;
76 set { throw new NotImplementedException(); }
79 /// <summary>
80 /// Adds an element with the provided key and value to the <see cref="T:System.Collections.IDictionary"/> object.
81 /// </summary>
82 /// <param name="key">The <see cref="T:System.Object"/> to use as the key of the element to add.</param>
83 /// <param name="value">The <see cref="T:System.Object"/> to use as the value of the element to add.</param>
84 /// <exception cref="T:System.ArgumentNullException">
85 /// <paramref name="key"/> is null. </exception>
86 /// <exception cref="T:System.ArgumentException">An element with the same key already exists in the <see cref="T:System.Collections.IDictionary"/> object. </exception>
87 /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.IDictionary"/> is read-only.-or- The <see cref="T:System.Collections.IDictionary"/> has a fixed size. </exception>
88 public void Add(object key, object value)
90 throw new NotImplementedException();
93 /// <summary>
94 /// Removes all elements from the <see cref="T:System.Collections.IDictionary"/> object.
95 /// </summary>
96 /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.IDictionary"/> object is read-only. </exception>
97 public void Clear()
99 throw new NotImplementedException();
102 /// <summary>
103 /// Returns an <see cref="T:System.Collections.IDictionaryEnumerator"/> object for the <see cref="T:System.Collections.IDictionary"/> object.
104 /// </summary>
105 /// <returns>
106 /// An <see cref="T:System.Collections.IDictionaryEnumerator"/> object for the <see cref="T:System.Collections.IDictionary"/> object.
107 /// </returns>
108 IDictionaryEnumerator IDictionary.GetEnumerator()
110 return new DictionaryEntryEnumeratorAdapter( properties.GetEnumerator() );
113 /// <summary>
114 /// Removes the element with the specified key from the <see cref="T:System.Collections.IDictionary"/> object.
115 /// </summary>
116 /// <param name="key">The key of the element to remove.</param>
117 /// <exception cref="T:System.ArgumentNullException">
118 /// <paramref name="key"/> is null. </exception>
119 /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.IDictionary"/> object is read-only.-or- The <see cref="T:System.Collections.IDictionary"/> has a fixed size. </exception>
120 public void Remove(object key)
124 /// <summary>
125 /// Gets an <see cref="T:System.Collections.ICollection"/> object containing the keys of the <see cref="T:System.Collections.IDictionary"/> object.
126 /// </summary>
127 /// <value></value>
128 /// <returns>An <see cref="T:System.Collections.ICollection"/> object containing the keys of the <see cref="T:System.Collections.IDictionary"/> object.</returns>
129 public ICollection Keys
131 get { return properties.Keys; }
134 /// <summary>
135 /// Gets an <see cref="T:System.Collections.ICollection"/> object containing the values in the <see cref="T:System.Collections.IDictionary"/> object.
136 /// </summary>
137 /// <value></value>
138 /// <returns>An <see cref="T:System.Collections.ICollection"/> object containing the values in the <see cref="T:System.Collections.IDictionary"/> object.</returns>
139 public ICollection Values
141 get { return properties.Values; }
144 /// <summary>
145 /// Gets a value indicating whether the <see cref="T:System.Collections.IDictionary"/> object is read-only.
146 /// </summary>
147 /// <value></value>
148 /// <returns>true if the <see cref="T:System.Collections.IDictionary"/> object is read-only; otherwise, false.</returns>
149 public bool IsReadOnly
151 get { return true; }
154 /// <summary>
155 /// Gets a value indicating whether the <see cref="T:System.Collections.IDictionary"/> object has a fixed size.
156 /// </summary>
157 /// <value></value>
158 /// <returns>true if the <see cref="T:System.Collections.IDictionary"/> object has a fixed size; otherwise, false.</returns>
159 public bool IsFixedSize
161 get { throw new NotImplementedException(); }
164 /// <summary>
165 /// Copies the elements of the <see cref="T:System.Collections.ICollection"/> to an <see cref="T:System.Array"/>, starting at a particular <see cref="T:System.Array"/> index.
166 /// </summary>
167 /// <param name="array">The one-dimensional <see cref="T:System.Array"/> that is the destination of the elements copied from <see cref="T:System.Collections.ICollection"/>. The <see cref="T:System.Array"/> must have zero-based indexing.</param>
168 /// <param name="index">The zero-based index in <paramref name="array"/> at which copying begins.</param>
169 /// <exception cref="T:System.ArgumentNullException">
170 /// <paramref name="array"/> is null. </exception>
171 /// <exception cref="T:System.ArgumentOutOfRangeException">
172 /// <paramref name="index"/> is less than zero. </exception>
173 /// <exception cref="T:System.ArgumentException">
174 /// <paramref name="array"/> is multidimensional.-or- <paramref name="index"/> is equal to or greater than the length of <paramref name="array"/>.-or- The number of elements in the source <see cref="T:System.Collections.ICollection"/> is greater than the available space from <paramref name="index"/> to the end of the destination <paramref name="array"/>. </exception>
175 /// <exception cref="T:System.ArgumentException">The type of the source <see cref="T:System.Collections.ICollection"/> cannot be cast automatically to the type of the destination <paramref name="array"/>. </exception>
176 public void CopyTo(Array array, int index)
178 throw new NotImplementedException();
181 /// <summary>
182 /// Gets the number of elements contained in the <see cref="T:System.Collections.ICollection"/>.
183 /// </summary>
184 /// <value></value>
185 /// <returns>The number of elements contained in the <see cref="T:System.Collections.ICollection"/>.</returns>
186 public int Count
188 get { return properties.Count; }
191 /// <summary>
192 /// Gets an object that can be used to synchronize access to the <see cref="T:System.Collections.ICollection"/>.
193 /// </summary>
194 /// <value></value>
195 /// <returns>An object that can be used to synchronize access to the <see cref="T:System.Collections.ICollection"/>.</returns>
196 public object SyncRoot
198 get { return properties; }
201 /// <summary>
202 /// Gets a value indicating whether access to the <see cref="T:System.Collections.ICollection"/> is synchronized (thread safe).
203 /// </summary>
204 /// <value></value>
205 /// <returns>true if access to the <see cref="T:System.Collections.ICollection"/> is synchronized (thread safe); otherwise, false.</returns>
206 public bool IsSynchronized
208 get { return false; }
211 /// <summary>
212 /// Returns an enumerator that iterates through a collection.
213 /// </summary>
214 /// <returns>
215 /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
216 /// </returns>
217 public IEnumerator GetEnumerator()
219 return new DictionaryEntryEnumeratorAdapter( properties.GetEnumerator() );
222 class DictionaryEntryEnumeratorAdapter : IDictionaryEnumerator
224 private readonly IDictionaryEnumerator enumerator;
225 private KeyValuePair<string, object> current;
227 public DictionaryEntryEnumeratorAdapter(IDictionaryEnumerator enumerator)
229 this.enumerator = enumerator;
232 public object Key
234 get { return current.Key; }
237 public object Value
239 get { return current.Value; }
242 public DictionaryEntry Entry
244 get { return new DictionaryEntry(Key, Value); }
247 public bool MoveNext()
249 return enumerator.MoveNext();
252 public void Reset()
254 enumerator.Reset();
257 public object Current
259 get
262 current = (KeyValuePair<string, object>) enumerator.Current;
263 return new DictionaryEntry(Key, Value);