// Copyright 2004-2008 Castle Project - http://www.castleproject.org/ // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. using System; using System.Collections; using System.Collections.Generic; using System.Reflection; namespace DevDefined.OAuth.Utility { /// /// Pendent /// public class ReflectionBasedDictionaryAdapter : IDictionary { readonly Dictionary _properties = new Dictionary(StringComparer.OrdinalIgnoreCase); /// /// Initializes a new instance of the class. /// /// The target. public ReflectionBasedDictionaryAdapter(object target) { if (target == null) { throw new ArgumentNullException("target"); } Type targetType = target.GetType(); foreach (PropertyInfo property in targetType.GetProperties(BindingFlags.Public | BindingFlags.Instance)) { if (!property.CanRead) continue; object value = property.GetValue(target, null); _properties[property.Name] = value; } } /// /// Determines whether the object contains an element with the specified key. /// /// The key to locate in the object. /// /// true if the contains an element with the key; otherwise, false. /// /// /// is null. public bool Contains(object key) { return _properties.ContainsKey(key.ToString()); } /// /// Gets or sets the with the specified key. /// /// public object this[object key] { get { object value; _properties.TryGetValue(key.ToString(), out value); return value; } set { throw new NotImplementedException(); } } /// /// Adds an element with the provided key and value to the object. /// /// The to use as the key of the element to add. /// The to use as the value of the element to add. /// /// is null. /// An element with the same key already exists in the object. /// The is read-only.-or- The has a fixed size. public void Add(object key, object value) { throw new NotImplementedException(); } /// /// Removes all elements from the object. /// /// The object is read-only. public void Clear() { throw new NotImplementedException(); } /// /// Returns an object for the object. /// /// /// An object for the object. /// IDictionaryEnumerator IDictionary.GetEnumerator() { return new DictionaryEntryEnumeratorAdapter(_properties.GetEnumerator()); } /// /// Removes the element with the specified key from the object. /// /// The key of the element to remove. /// /// is null. /// The object is read-only.-or- The has a fixed size. public void Remove(object key) { } /// /// Gets an object containing the keys of the object. /// /// /// An object containing the keys of the object. public ICollection Keys { get { return _properties.Keys; } } /// /// Gets an object containing the values in the object. /// /// /// An object containing the values in the object. public ICollection Values { get { return _properties.Values; } } /// /// Gets a value indicating whether the object is read-only. /// /// /// true if the object is read-only; otherwise, false. public bool IsReadOnly { get { return true; } } /// /// Gets a value indicating whether the object has a fixed size. /// /// /// true if the object has a fixed size; otherwise, false. public bool IsFixedSize { get { throw new NotImplementedException(); } } /// /// Copies the elements of the to an , starting at a particular index. /// /// The one-dimensional that is the destination of the elements copied from . The must have zero-based indexing. /// The zero-based index in at which copying begins. /// /// is null. /// /// is less than zero. /// /// is multidimensional.-or- is equal to or greater than the length of .-or- The number of elements in the source is greater than the available space from to the end of the destination . /// The type of the source cannot be cast automatically to the type of the destination . public void CopyTo(Array array, int index) { throw new NotImplementedException(); } /// /// Gets the number of elements contained in the . /// /// /// The number of elements contained in the . public int Count { get { return _properties.Count; } } /// /// Gets an object that can be used to synchronize access to the . /// /// /// An object that can be used to synchronize access to the . public object SyncRoot { get { return _properties; } } /// /// Gets a value indicating whether access to the is synchronized (thread safe). /// /// /// true if access to the is synchronized (thread safe); otherwise, false. public bool IsSynchronized { get { return false; } } /// /// Returns an enumerator that iterates through a collection. /// /// /// An object that can be used to iterate through the collection. /// public IEnumerator GetEnumerator() { return new DictionaryEntryEnumeratorAdapter(_properties.GetEnumerator()); } #region Nested type: DictionaryEntryEnumeratorAdapter class DictionaryEntryEnumeratorAdapter : IDictionaryEnumerator { readonly IDictionaryEnumerator _enumerator; KeyValuePair _current; public DictionaryEntryEnumeratorAdapter(IDictionaryEnumerator enumerator) { _enumerator = enumerator; } public object Key { get { return _current.Key; } } public object Value { get { return _current.Value; } } public DictionaryEntry Entry { get { return new DictionaryEntry(Key, Value); } } public bool MoveNext() { bool moved = _enumerator.MoveNext(); if (moved) { _current = (KeyValuePair) _enumerator.Current; } return moved; } public void Reset() { _enumerator.Reset(); } public object Current { get { return new DictionaryEntry(Key, Value); } } } #endregion } }