diff --git a/source/MongoDB/Document.cs b/source/MongoDB/Document.cs index c09615e0..e129360d 100644 --- a/source/MongoDB/Document.cs +++ b/source/MongoDB/Document.cs @@ -22,16 +22,16 @@ public class Document : IDictionary, IDictionary, IXmlSerializabl /// /// Initializes a new instance of the class. /// - public Document(){ + public Document(){ _dictionary = new Dictionary(); _orderedKeys = new List(); } - - /// - /// Initialize a new instance of the class with an optional key sorter. + + /// + /// Initialize a new instance of the class with an optional key sorter. /// - public Document(IComparer keyComparer) - :this() + public Document(IComparer keyComparer) + :this() { if(keyComparer == null) throw new ArgumentNullException("keyComparer"); @@ -431,17 +431,17 @@ void IDictionary.Remove(object key) /// Copies to items to destinationDocument. /// /// The destination document. - 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]; + } } /// /// Removes the first occurrence of a specific object from the . @@ -614,16 +614,16 @@ bool IDictionary.IsFixedSize } return hash; } - - /// - /// Returns an enumerator that iterates through a collection. - /// - /// - /// An object that can be used to iterate through the collection. - /// - IEnumerator IEnumerable.GetEnumerator(){ - return GetEnumerator(); - } + + /// + /// Returns an enumerator that iterates through a collection. + /// + /// + /// An object that can be used to iterate through the collection. + /// + IEnumerator IEnumerable.GetEnumerator(){ + return GetEnumerator(); + } /// /// Returns an enumerator that iterates through the collection. @@ -631,8 +631,60 @@ bool IDictionary.IsFixedSize /// /// A that can be used to iterate through the collection. /// - public IEnumerator> GetEnumerator(){ - return _orderedKeys.Select(orderedKey => new KeyValuePair(orderedKey, _dictionary[orderedKey])).GetEnumerator(); + public IEnumerator> GetEnumerator() + { + return new Enumerator(this); + } + + private class Enumerator : IEnumerator>, IDictionaryEnumerator + { + private readonly Document _doc; + private readonly IEnumerator _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 Current + { + get { return new KeyValuePair(_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]); } + } } /// @@ -723,4 +775,4 @@ void IXmlSerializable.WriteXml(XmlWriter writer) } } } -} +}