55using System . Linq ;
66using System . Linq . Expressions ;
77
8- namespace Gameloop . Vdf
8+ namespace Gameloop . Vdf . Linq
99{
10- public class VObject : VToken
10+ public class VObject : VToken , IDictionary < string , VToken >
1111 {
1212 private readonly List < VProperty > _children ;
1313
@@ -44,21 +44,26 @@ public VToken this[string key]
4444 {
4545 get
4646 {
47- if ( ! TryGetValue ( key , out VProperty result ) )
47+ if ( ! TryGetValue ( key , out VToken result ) )
4848 throw new KeyNotFoundException ( "The given key was not present." ) ;
4949
50- return result . Value ;
50+ return result ;
5151 }
5252
5353 set
5454 {
55- if ( TryGetValue ( key , out VProperty result ) )
56- result . Value = value ;
55+ VProperty prop = _children . FirstOrDefault ( x => x . Key == key ) ;
56+ if ( prop != null )
57+ prop . Value = value ;
5758 else
5859 Add ( key , value ) ;
5960 }
6061 }
6162
63+ ICollection < string > IDictionary < string , VToken > . Keys => _children . Select ( x => x . Key ) . ToList ( ) ;
64+
65+ ICollection < VToken > IDictionary < string , VToken > . Values => throw new NotImplementedException ( ) ;
66+
6267 public override IEnumerable < VProperty > Children ( )
6368 {
6469 return _children ;
@@ -102,10 +107,10 @@ public bool Remove(string key)
102107 return _children . RemoveAll ( x => x . Key == key ) != 0 ;
103108 }
104109
105- public bool TryGetValue ( string key , out VProperty value )
110+ public bool TryGetValue ( string key , out VToken value )
106111 {
107- value = _children . FirstOrDefault ( x => x . Key == key ) ;
108- return value != null ;
112+ value = _children . FirstOrDefault ( x => x . Key == key ) ? . Value ;
113+ return ( value != null ) ;
109114 }
110115
111116 public void RemoveAt ( string key )
@@ -123,6 +128,61 @@ public override void WriteTo(VdfWriter writer)
123128 writer . WriteObjectEnd ( ) ;
124129 }
125130
131+ #region ICollection<KeyValuePair<string,JToken>> Members
132+
133+ public IEnumerator < KeyValuePair < string , VToken > > GetEnumerator ( )
134+ {
135+ foreach ( VProperty property in _children )
136+ yield return new KeyValuePair < string , VToken > ( property . Key , property . Value ) ;
137+ }
138+
139+ void ICollection < KeyValuePair < string , VToken > > . Add ( KeyValuePair < string , VToken > item )
140+ {
141+ Add ( new VProperty ( item . Key , item . Value ) ) ;
142+ }
143+
144+ void ICollection < KeyValuePair < string , VToken > > . Clear ( )
145+ {
146+ _children . Clear ( ) ;
147+ }
148+
149+ bool ICollection < KeyValuePair < string , VToken > > . Contains ( KeyValuePair < string , VToken > item )
150+ {
151+ VProperty property = _children . FirstOrDefault ( x => x . Key == item . Key ) ;
152+ if ( property == null )
153+ return false ;
154+
155+ return ( property . Value == item . Value ) ;
156+ }
157+
158+ void ICollection < KeyValuePair < string , VToken > > . CopyTo ( KeyValuePair < string , VToken > [ ] array , int arrayIndex )
159+ {
160+ if ( array == null )
161+ throw new ArgumentNullException ( nameof ( array ) ) ;
162+ if ( arrayIndex < 0 )
163+ throw new ArgumentOutOfRangeException ( nameof ( arrayIndex ) , "arrayIndex is less than 0." ) ;
164+ if ( arrayIndex >= array . Length && arrayIndex != 0 )
165+ throw new ArgumentException ( "arrayIndex is equal to or greater than the length of array." ) ;
166+ if ( Count > array . Length - arrayIndex )
167+ throw new ArgumentException ( "The number of elements in the source JObject is greater than the available space from arrayIndex to the end of the destination array." ) ;
168+
169+ for ( int index = 0 ; index < _children . Count ; index ++ )
170+ array [ arrayIndex + index ] = new KeyValuePair < string , VToken > ( _children [ index ] . Key , _children [ index ] . Value ) ;
171+ }
172+
173+ bool ICollection < KeyValuePair < string , VToken > > . IsReadOnly => false ;
174+
175+ bool ICollection < KeyValuePair < string , VToken > > . Remove ( KeyValuePair < string , VToken > item )
176+ {
177+ if ( ! ( ( ICollection < KeyValuePair < string , VToken > > ) this ) . Contains ( item ) )
178+ return false ;
179+
180+ ( ( IDictionary < string , VToken > ) this ) . Remove ( item . Key ) ;
181+ return true ;
182+ }
183+
184+ #endregion
185+
126186 protected override DynamicMetaObject GetMetaObject ( Expression parameter )
127187 {
128188 return new DynamicProxyMetaObject < VObject > ( parameter , this , new VObjectDynamicProxy ( ) ) ;
0 commit comments