1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System
.Collections
;
19 using System
.Reflection
;
20 using System
.Collections
.Generic
;
25 public class ReflectionBasedDictionaryAdapter
: IDictionary
27 private readonly Dictionary
<string,object> properties
= new Dictionary
<string, object>(StringComparer
.InvariantCultureIgnoreCase
);
30 /// Initializes a new instance of the <see cref="ReflectionBasedDictionaryAdapter"/> class.
32 /// <param name="target">The target.</param>
33 public ReflectionBasedDictionaryAdapter(object target
)
37 throw new ArgumentNullException("target");
40 Type targetType
= target
.GetType();
42 foreach(PropertyInfo property
in targetType
.GetProperties(BindingFlags
.Public
| BindingFlags
.Instance
))
44 if (!property
.CanRead
) continue;
45 object value = property
.GetValue(target
, null);
47 properties
[property
.Name
] = value;
52 /// Determines whether the <see cref="T:System.Collections.IDictionary"/> object contains an element with the specified key.
54 /// <param name="key">The key to locate in the <see cref="T:System.Collections.IDictionary"/> object.</param>
56 /// true if the <see cref="T:System.Collections.IDictionary"/> contains an element with the key; otherwise, false.
58 /// <exception cref="T:System.ArgumentNullException">
59 /// <paramref name="key"/> is null. </exception>
60 public bool Contains(object key
)
62 return properties
.ContainsKey(key
.ToString());
66 /// Gets or sets the <see cref="System.Object"/> with the specified key.
69 public object this[object key
]
74 properties
.TryGetValue(key
.ToString(), out value);
77 set { throw new NotImplementedException(); }
81 /// Adds an element with the provided key and value to the <see cref="T:System.Collections.IDictionary"/> object.
83 /// <param name="key">The <see cref="T:System.Object"/> to use as the key of the element to add.</param>
84 /// <param name="value">The <see cref="T:System.Object"/> to use as the value of the element to add.</param>
85 /// <exception cref="T:System.ArgumentNullException">
86 /// <paramref name="key"/> is null. </exception>
87 /// <exception cref="T:System.ArgumentException">An element with the same key already exists in the <see cref="T:System.Collections.IDictionary"/> object. </exception>
88 /// <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>
89 public void Add(object key
, object value)
91 throw new NotImplementedException();
95 /// Removes all elements from the <see cref="T:System.Collections.IDictionary"/> object.
97 /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.IDictionary"/> object is read-only. </exception>
100 throw new NotImplementedException();
104 /// Returns an <see cref="T:System.Collections.IDictionaryEnumerator"/> object for the <see cref="T:System.Collections.IDictionary"/> object.
107 /// An <see cref="T:System.Collections.IDictionaryEnumerator"/> object for the <see cref="T:System.Collections.IDictionary"/> object.
109 IDictionaryEnumerator IDictionary
.GetEnumerator()
111 return new DictionaryEntryEnumeratorAdapter( properties
.GetEnumerator() );
115 /// Removes the element with the specified key from the <see cref="T:System.Collections.IDictionary"/> object.
117 /// <param name="key">The key of the element to remove.</param>
118 /// <exception cref="T:System.ArgumentNullException">
119 /// <paramref name="key"/> is null. </exception>
120 /// <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>
121 public void Remove(object key
)
126 /// Gets an <see cref="T:System.Collections.ICollection"/> object containing the keys of the <see cref="T:System.Collections.IDictionary"/> object.
129 /// <returns>An <see cref="T:System.Collections.ICollection"/> object containing the keys of the <see cref="T:System.Collections.IDictionary"/> object.</returns>
130 public ICollection Keys
132 get { return properties.Keys; }
136 /// Gets an <see cref="T:System.Collections.ICollection"/> object containing the values in the <see cref="T:System.Collections.IDictionary"/> object.
139 /// <returns>An <see cref="T:System.Collections.ICollection"/> object containing the values in the <see cref="T:System.Collections.IDictionary"/> object.</returns>
140 public ICollection Values
142 get { return properties.Values; }
146 /// Gets a value indicating whether the <see cref="T:System.Collections.IDictionary"/> object is read-only.
149 /// <returns>true if the <see cref="T:System.Collections.IDictionary"/> object is read-only; otherwise, false.</returns>
150 public bool IsReadOnly
156 /// Gets a value indicating whether the <see cref="T:System.Collections.IDictionary"/> object has a fixed size.
159 /// <returns>true if the <see cref="T:System.Collections.IDictionary"/> object has a fixed size; otherwise, false.</returns>
160 public bool IsFixedSize
162 get { throw new NotImplementedException(); }
166 /// 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.
168 /// <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>
169 /// <param name="index">The zero-based index in <paramref name="array"/> at which copying begins.</param>
170 /// <exception cref="T:System.ArgumentNullException">
171 /// <paramref name="array"/> is null. </exception>
172 /// <exception cref="T:System.ArgumentOutOfRangeException">
173 /// <paramref name="index"/> is less than zero. </exception>
174 /// <exception cref="T:System.ArgumentException">
175 /// <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>
176 /// <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>
177 public void CopyTo(Array array
, int index
)
179 throw new NotImplementedException();
183 /// Gets the number of elements contained in the <see cref="T:System.Collections.ICollection"/>.
186 /// <returns>The number of elements contained in the <see cref="T:System.Collections.ICollection"/>.</returns>
189 get { return properties.Count; }
193 /// Gets an object that can be used to synchronize access to the <see cref="T:System.Collections.ICollection"/>.
196 /// <returns>An object that can be used to synchronize access to the <see cref="T:System.Collections.ICollection"/>.</returns>
197 public object SyncRoot
199 get { return properties; }
203 /// Gets a value indicating whether access to the <see cref="T:System.Collections.ICollection"/> is synchronized (thread safe).
206 /// <returns>true if access to the <see cref="T:System.Collections.ICollection"/> is synchronized (thread safe); otherwise, false.</returns>
207 public bool IsSynchronized
209 get { return false; }
213 /// Returns an enumerator that iterates through a collection.
216 /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
218 public IEnumerator
GetEnumerator()
220 return new DictionaryEntryEnumeratorAdapter( properties
.GetEnumerator() );
223 class DictionaryEntryEnumeratorAdapter
: IDictionaryEnumerator
225 private readonly IDictionaryEnumerator enumerator
;
226 private KeyValuePair
<string, object> current
;
228 public DictionaryEntryEnumeratorAdapter(IDictionaryEnumerator enumerator
)
230 this.enumerator
= enumerator
;
235 get { return current.Key; }
240 get { return current.Value; }
243 public DictionaryEntry Entry
245 get { return new DictionaryEntry(Key, Value); }
248 public bool MoveNext()
250 bool moved
= enumerator
.MoveNext();
254 current
= (KeyValuePair
<string, object>)enumerator
.Current
;
265 public object Current
267 get { return new DictionaryEntry(Key, Value); }