Skip to content

Commit 83d3951

Browse files
committed
Implement the rest of the IDictionary<string, object> methods.
1 parent 249efdf commit 83d3951

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

src/CsvHelper/FastDynamicObject.cs

+27-22
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.ComponentModel;
45
using System.Dynamic;
6+
using System.Linq;
57
using System.Linq.Expressions;
68
using System.Reflection;
79

810
namespace CsvHelper;
911

10-
internal class FastDynamicObject : IDictionary<string, object>, IDynamicMetaObjectProvider
12+
internal class FastDynamicObject : IDynamicMetaObjectProvider, IDictionary<string, object>
1113
{
1214
private readonly Dictionary<string, object> dict;
1315

@@ -30,24 +32,24 @@ object IDictionary<string, object>.this[string key]
3032

3133
set
3234
{
33-
dict[key] = value;
35+
SetValue(key, value);
3436
}
3537
}
3638

37-
object SetValue(string name, object value)
38-
{
39-
dict[name] = value;
39+
ICollection<string> IDictionary<string, object>.Keys => dict.Keys;
4040

41-
return value;
42-
}
41+
ICollection<object> IDictionary<string, object>.Values => dict.Values;
4342

44-
ICollection<string> IDictionary<string, object>.Keys => throw new NotSupportedException();
43+
int ICollection<KeyValuePair<string, object>>.Count => dict.Count;
4544

46-
ICollection<object> IDictionary<string, object>.Values => throw new NotSupportedException();
45+
bool ICollection<KeyValuePair<string, object>>.IsReadOnly => false;
4746

48-
int ICollection<KeyValuePair<string, object>>.Count => throw new NotSupportedException();
47+
object SetValue(string key, object value)
48+
{
49+
dict[key] = value;
4950

50-
bool ICollection<KeyValuePair<string, object>>.IsReadOnly => throw new NotSupportedException();
51+
return value;
52+
}
5153

5254
DynamicMetaObject IDynamicMetaObjectProvider.GetMetaObject(Expression parameter)
5355
{
@@ -56,57 +58,60 @@ DynamicMetaObject IDynamicMetaObjectProvider.GetMetaObject(Expression parameter)
5658

5759
void IDictionary<string, object>.Add(string key, object value)
5860
{
59-
throw new NotSupportedException();
61+
SetValue(key, value);
6062
}
6163

6264
void ICollection<KeyValuePair<string, object>>.Add(KeyValuePair<string, object> item)
6365
{
64-
throw new NotSupportedException();
66+
SetValue(item.Key, item.Value);
6567
}
6668

6769
void ICollection<KeyValuePair<string, object>>.Clear()
6870
{
69-
throw new NotSupportedException();
71+
dict.Clear();
7072
}
7173

7274
bool ICollection<KeyValuePair<string, object>>.Contains(KeyValuePair<string, object> item)
7375
{
74-
throw new NotSupportedException();
76+
return dict.Contains(item);
7577
}
7678

7779
bool IDictionary<string, object>.ContainsKey(string key)
7880
{
79-
throw new NotSupportedException();
81+
return dict.ContainsKey(key);
8082
}
8183

8284
void ICollection<KeyValuePair<string, object>>.CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
8385
{
84-
throw new NotSupportedException();
86+
foreach (var item in array)
87+
{
88+
SetValue(item.Key, item.Value);
89+
}
8590
}
8691

8792
IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()
8893
{
89-
throw new NotSupportedException();
94+
return dict.GetEnumerator();
9095
}
9196

9297
IEnumerator IEnumerable.GetEnumerator()
9398
{
94-
throw new NotSupportedException();
99+
return dict.GetEnumerator();
95100
}
96101

97102
bool IDictionary<string, object>.Remove(string key)
98103
{
99-
throw new NotSupportedException();
104+
return dict.Remove(key);
100105
}
101106

102107
bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> item)
103108
{
104-
throw new NotSupportedException();
109+
return dict.Remove(item.Key);
105110
}
106111

107112
bool IDictionary<string, object>.TryGetValue(string key, out object value)
108113
{
109-
throw new NotSupportedException();
114+
return dict.TryGetValue(key, out value);
110115
}
111116

112117
private class FastDynamicMetaObject : DynamicMetaObject

0 commit comments

Comments
 (0)