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
.Collections
.Specialized
;
20 using System
.Reflection
;
25 public class ReflectionBasedDictionaryAdapter
: IDictionary
27 private readonly IDictionary properties
= new HybridDictionary(true);
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 object value = property
.GetValue(target
, null);
46 properties
[property
.Name
] = value;
51 /// Determines whether the <see cref="T:System.Collections.IDictionary"/> object contains an element with the specified key.
53 /// <param name="key">The key to locate in the <see cref="T:System.Collections.IDictionary"/> object.</param>
55 /// true if the <see cref="T:System.Collections.IDictionary"/> contains an element with the key; otherwise, false.
57 /// <exception cref="T:System.ArgumentNullException">
58 /// <paramref name="key"/> is null. </exception>
59 public bool Contains(object key
)
61 return properties
.Contains(key
);
65 /// Gets or sets the <see cref="System.Object"/> with the specified key.
68 public object this[object key
]
72 return properties
[key
];
74 set { throw new NotImplementedException(); }
78 /// Adds an element with the provided key and value to the <see cref="T:System.Collections.IDictionary"/> object.
80 /// <param name="key">The <see cref="T:System.Object"/> to use as the key of the element to add.</param>
81 /// <param name="value">The <see cref="T:System.Object"/> to use as the value of the element to add.</param>
82 /// <exception cref="T:System.ArgumentNullException">
83 /// <paramref name="key"/> is null. </exception>
84 /// <exception cref="T:System.ArgumentException">An element with the same key already exists in the <see cref="T:System.Collections.IDictionary"/> object. </exception>
85 /// <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>
86 public void Add(object key
, object value)
88 throw new NotImplementedException();
92 /// Removes all elements from the <see cref="T:System.Collections.IDictionary"/> object.
94 /// <exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.IDictionary"/> object is read-only. </exception>
97 throw new NotImplementedException();
101 /// Returns an <see cref="T:System.Collections.IDictionaryEnumerator"/> object for the <see cref="T:System.Collections.IDictionary"/> object.
104 /// An <see cref="T:System.Collections.IDictionaryEnumerator"/> object for the <see cref="T:System.Collections.IDictionary"/> object.
106 IDictionaryEnumerator IDictionary
.GetEnumerator()
108 return properties
.GetEnumerator();
112 /// Removes the element with the specified key from the <see cref="T:System.Collections.IDictionary"/> object.
114 /// <param name="key">The key of the element to remove.</param>
115 /// <exception cref="T:System.ArgumentNullException">
116 /// <paramref name="key"/> is null. </exception>
117 /// <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>
118 public void Remove(object key
)
123 /// Gets an <see cref="T:System.Collections.ICollection"/> object containing the keys of the <see cref="T:System.Collections.IDictionary"/> object.
126 /// <returns>An <see cref="T:System.Collections.ICollection"/> object containing the keys of the <see cref="T:System.Collections.IDictionary"/> object.</returns>
127 public ICollection Keys
129 get { return properties.Keys; }
133 /// Gets an <see cref="T:System.Collections.ICollection"/> object containing the values in the <see cref="T:System.Collections.IDictionary"/> object.
136 /// <returns>An <see cref="T:System.Collections.ICollection"/> object containing the values in the <see cref="T:System.Collections.IDictionary"/> object.</returns>
137 public ICollection Values
139 get { return properties.Values; }
143 /// Gets a value indicating whether the <see cref="T:System.Collections.IDictionary"/> object is read-only.
146 /// <returns>true if the <see cref="T:System.Collections.IDictionary"/> object is read-only; otherwise, false.</returns>
147 public bool IsReadOnly
153 /// Gets a value indicating whether the <see cref="T:System.Collections.IDictionary"/> object has a fixed size.
156 /// <returns>true if the <see cref="T:System.Collections.IDictionary"/> object has a fixed size; otherwise, false.</returns>
157 public bool IsFixedSize
159 get { throw new NotImplementedException(); }
163 /// 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.
165 /// <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>
166 /// <param name="index">The zero-based index in <paramref name="array"/> at which copying begins.</param>
167 /// <exception cref="T:System.ArgumentNullException">
168 /// <paramref name="array"/> is null. </exception>
169 /// <exception cref="T:System.ArgumentOutOfRangeException">
170 /// <paramref name="index"/> is less than zero. </exception>
171 /// <exception cref="T:System.ArgumentException">
172 /// <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>
173 /// <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>
174 public void CopyTo(Array array
, int index
)
176 throw new NotImplementedException();
180 /// Gets the number of elements contained in the <see cref="T:System.Collections.ICollection"/>.
183 /// <returns>The number of elements contained in the <see cref="T:System.Collections.ICollection"/>.</returns>
186 get { return properties.Count; }
190 /// Gets an object that can be used to synchronize access to the <see cref="T:System.Collections.ICollection"/>.
193 /// <returns>An object that can be used to synchronize access to the <see cref="T:System.Collections.ICollection"/>.</returns>
194 public object SyncRoot
196 get { return properties; }
200 /// Gets a value indicating whether access to the <see cref="T:System.Collections.ICollection"/> is synchronized (thread safe).
203 /// <returns>true if access to the <see cref="T:System.Collections.ICollection"/> is synchronized (thread safe); otherwise, false.</returns>
204 public bool IsSynchronized
206 get { return false; }
210 /// Returns an enumerator that iterates through a collection.
213 /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
215 public IEnumerator
GetEnumerator()
217 return properties
.GetEnumerator();