1
1
using System ;
2
2
using System . Collections ;
3
3
using System . Collections . Generic ;
4
+ using System . ComponentModel ;
4
5
using System . Dynamic ;
6
+ using System . Linq ;
5
7
using System . Linq . Expressions ;
6
8
using System . Reflection ;
7
9
8
10
namespace CsvHelper ;
9
11
10
- internal class FastDynamicObject : IDictionary < string , object > , IDynamicMetaObjectProvider
12
+ internal class FastDynamicObject : IDynamicMetaObjectProvider , IDictionary < string , object >
11
13
{
12
14
private readonly Dictionary < string , object > dict ;
13
15
@@ -30,24 +32,24 @@ object IDictionary<string, object>.this[string key]
30
32
31
33
set
32
34
{
33
- dict [ key ] = value ;
35
+ SetValue ( key , value ) ;
34
36
}
35
37
}
36
38
37
- object SetValue ( string name , object value )
38
- {
39
- dict [ name ] = value ;
39
+ ICollection < string > IDictionary < string , object > . Keys => dict . Keys ;
40
40
41
- return value ;
42
- }
41
+ ICollection < object > IDictionary < string , object > . Values => dict . Values ;
43
42
44
- ICollection < string > IDictionary < string , object > . Keys => throw new NotSupportedException ( ) ;
43
+ int ICollection < KeyValuePair < string , object > > . Count => dict . Count ;
45
44
46
- ICollection < object > IDictionary < string , object > . Values => throw new NotSupportedException ( ) ;
45
+ bool ICollection < KeyValuePair < string , object > > . IsReadOnly => false ;
47
46
48
- int ICollection < KeyValuePair < string , object > > . Count => throw new NotSupportedException ( ) ;
47
+ object SetValue ( string key , object value )
48
+ {
49
+ dict [ key ] = value ;
49
50
50
- bool ICollection < KeyValuePair < string , object > > . IsReadOnly => throw new NotSupportedException ( ) ;
51
+ return value ;
52
+ }
51
53
52
54
DynamicMetaObject IDynamicMetaObjectProvider . GetMetaObject ( Expression parameter )
53
55
{
@@ -56,57 +58,60 @@ DynamicMetaObject IDynamicMetaObjectProvider.GetMetaObject(Expression parameter)
56
58
57
59
void IDictionary < string , object > . Add ( string key , object value )
58
60
{
59
- throw new NotSupportedException ( ) ;
61
+ SetValue ( key , value ) ;
60
62
}
61
63
62
64
void ICollection < KeyValuePair < string , object > > . Add ( KeyValuePair < string , object > item )
63
65
{
64
- throw new NotSupportedException ( ) ;
66
+ SetValue ( item . Key , item . Value ) ;
65
67
}
66
68
67
69
void ICollection < KeyValuePair < string , object > > . Clear ( )
68
70
{
69
- throw new NotSupportedException ( ) ;
71
+ dict . Clear ( ) ;
70
72
}
71
73
72
74
bool ICollection < KeyValuePair < string , object > > . Contains ( KeyValuePair < string , object > item )
73
75
{
74
- throw new NotSupportedException ( ) ;
76
+ return dict . Contains ( item ) ;
75
77
}
76
78
77
79
bool IDictionary < string , object > . ContainsKey ( string key )
78
80
{
79
- throw new NotSupportedException ( ) ;
81
+ return dict . ContainsKey ( key ) ;
80
82
}
81
83
82
84
void ICollection < KeyValuePair < string , object > > . CopyTo ( KeyValuePair < string , object > [ ] array , int arrayIndex )
83
85
{
84
- throw new NotSupportedException ( ) ;
86
+ foreach ( var item in array )
87
+ {
88
+ SetValue ( item . Key , item . Value ) ;
89
+ }
85
90
}
86
91
87
92
IEnumerator < KeyValuePair < string , object > > IEnumerable < KeyValuePair < string , object > > . GetEnumerator ( )
88
93
{
89
- throw new NotSupportedException ( ) ;
94
+ return dict . GetEnumerator ( ) ;
90
95
}
91
96
92
97
IEnumerator IEnumerable . GetEnumerator ( )
93
98
{
94
- throw new NotSupportedException ( ) ;
99
+ return dict . GetEnumerator ( ) ;
95
100
}
96
101
97
102
bool IDictionary < string , object > . Remove ( string key )
98
103
{
99
- throw new NotSupportedException ( ) ;
104
+ return dict . Remove ( key ) ;
100
105
}
101
106
102
107
bool ICollection < KeyValuePair < string , object > > . Remove ( KeyValuePair < string , object > item )
103
108
{
104
- throw new NotSupportedException ( ) ;
109
+ return dict . Remove ( item . Key ) ;
105
110
}
106
111
107
112
bool IDictionary < string , object > . TryGetValue ( string key , out object value )
108
113
{
109
- throw new NotSupportedException ( ) ;
114
+ return dict . TryGetValue ( key , out value ) ;
110
115
}
111
116
112
117
private class FastDynamicMetaObject : DynamicMetaObject
0 commit comments