Permalink
Browse files

implement IDictionaryEnumerator to make sure that it can be serialize…

…d/deserialized properly
  • Loading branch information...
1 parent 1157e20 commit 38418fb9eee13c1d3575e18087ab8361c42768d8 @testn testn committed Oct 21, 2010
Showing with 80 additions and 28 deletions.
  1. +80 −28 source/MongoDB/Document.cs
View
@@ -22,16 +22,16 @@ public class Document : IDictionary<string,object>, IDictionary, IXmlSerializabl
/// <summary>
/// Initializes a new instance of the <see cref="Document"/> class.
/// </summary>
- public Document(){
+ public Document(){
_dictionary = new Dictionary<string, object>();
_orderedKeys = new List<String>();
}
-
- /// <summary>
- /// Initialize a new instance of the <see cref="Document"/> class with an optional key sorter.
+
+ /// <summary>
+ /// Initialize a new instance of the <see cref="Document"/> class with an optional key sorter.
/// </summary>
- public Document(IComparer<string> keyComparer)
- :this()
+ public Document(IComparer<string> keyComparer)
+ :this()
{
if(keyComparer == null)
throw new ArgumentNullException("keyComparer");
@@ -431,17 +431,17 @@ void IDictionary.Remove(object key)
/// Copies to items to destinationDocument.
/// </summary>
/// <param name="destinationDocument">The destination document.</param>
- public void CopyTo(Document destinationDocument){
- if(destinationDocument == null)
- throw new ArgumentNullException("destinationDocument");
-
+ public void CopyTo(Document destinationDocument){
+ if(destinationDocument == null)
+ throw new ArgumentNullException("destinationDocument");
+
//Todo: Fix any accidental reordering issues.
- foreach(var key in _orderedKeys){
- if(destinationDocument.ContainsKey(key))
- destinationDocument.Remove(key);
- destinationDocument[key] = this[key];
- }
+ foreach(var key in _orderedKeys){
+ if(destinationDocument.ContainsKey(key))
+ destinationDocument.Remove(key);
+ destinationDocument[key] = this[key];
+ }
}
/// <summary>
/// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
@@ -614,25 +614,77 @@ bool IDictionary.IsFixedSize
}
return hash;
}
-
- /// <summary>
- /// Returns an enumerator that iterates through a collection.
- /// </summary>
- /// <returns>
- /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
- /// </returns>
- IEnumerator IEnumerable.GetEnumerator(){
- return GetEnumerator();
- }
+
+ /// <summary>
+ /// Returns an enumerator that iterates through a collection.
+ /// </summary>
+ /// <returns>
+ /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
+ /// </returns>
+ IEnumerator IEnumerable.GetEnumerator(){
+ return GetEnumerator();
+ }
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>
/// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
/// </returns>
- public IEnumerator<KeyValuePair<string, object>> GetEnumerator(){
- return _orderedKeys.Select(orderedKey => new KeyValuePair<string, object>(orderedKey, _dictionary[orderedKey])).GetEnumerator();
+ public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
+ {
+ return new Enumerator(this);
+ }
+
+ private class Enumerator : IEnumerator<KeyValuePair<string, object>>, IDictionaryEnumerator
+ {
+ private readonly Document _doc;
+ private readonly IEnumerator<string> _keysEnum;
+ public Enumerator(Document doc)
+ {
+ _doc = doc;
+ _keysEnum = doc._orderedKeys.GetEnumerator();
+ }
+
+ public void Dispose()
+ {
+ _keysEnum.Dispose();
+ }
+
+ public bool MoveNext()
+ {
+ return _keysEnum.MoveNext();
+ }
+
+ public void Reset()
+ {
+ _keysEnum.Reset();
+ }
+
+ public KeyValuePair<string, object> Current
+ {
+ get { return new KeyValuePair<string, object>(_keysEnum.Current, _doc[_keysEnum.Current]); }
+ }
+
+ object IEnumerator.Current
+ {
+ get { return Current; }
+ }
+
+ public object Key
+ {
+ get { return _keysEnum.Current; }
+ }
+
+ public object Value
+ {
+ get { return _doc[_keysEnum.Current]; }
+ }
+
+ public DictionaryEntry Entry
+ {
+ get { return new DictionaryEntry(_keysEnum.Current, _doc[_keysEnum.Current]); }
+ }
}
/// <summary>
@@ -723,4 +775,4 @@ void IXmlSerializable.WriteXml(XmlWriter writer)
}
}
}
-}
+}

0 comments on commit 38418fb

Please sign in to comment.