From 83d3951b71a7d0b4200b5ca8464beb6af16400a3 Mon Sep 17 00:00:00 2001 From: Josh Close Date: Fri, 26 Apr 2024 10:09:53 -0500 Subject: [PATCH] Implement the rest of the IDictionary methods. --- src/CsvHelper/FastDynamicObject.cs | 49 ++++++++++++++++-------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/src/CsvHelper/FastDynamicObject.cs b/src/CsvHelper/FastDynamicObject.cs index d5512005d..c53d317fd 100644 --- a/src/CsvHelper/FastDynamicObject.cs +++ b/src/CsvHelper/FastDynamicObject.cs @@ -1,13 +1,15 @@ using System; using System.Collections; using System.Collections.Generic; +using System.ComponentModel; using System.Dynamic; +using System.Linq; using System.Linq.Expressions; using System.Reflection; namespace CsvHelper; -internal class FastDynamicObject : IDictionary, IDynamicMetaObjectProvider +internal class FastDynamicObject : IDynamicMetaObjectProvider, IDictionary { private readonly Dictionary dict; @@ -30,24 +32,24 @@ public FastDynamicObject() set { - dict[key] = value; + SetValue(key, value); } } - object SetValue(string name, object value) - { - dict[name] = value; + ICollection IDictionary.Keys => dict.Keys; - return value; - } + ICollection IDictionary.Values => dict.Values; - ICollection IDictionary.Keys => throw new NotSupportedException(); + int ICollection>.Count => dict.Count; - ICollection IDictionary.Values => throw new NotSupportedException(); + bool ICollection>.IsReadOnly => false; - int ICollection>.Count => throw new NotSupportedException(); + object SetValue(string key, object value) + { + dict[key] = value; - bool ICollection>.IsReadOnly => throw new NotSupportedException(); + return value; + } DynamicMetaObject IDynamicMetaObjectProvider.GetMetaObject(Expression parameter) { @@ -56,57 +58,60 @@ DynamicMetaObject IDynamicMetaObjectProvider.GetMetaObject(Expression parameter) void IDictionary.Add(string key, object value) { - throw new NotSupportedException(); + SetValue(key, value); } void ICollection>.Add(KeyValuePair item) { - throw new NotSupportedException(); + SetValue(item.Key, item.Value); } void ICollection>.Clear() { - throw new NotSupportedException(); + dict.Clear(); } bool ICollection>.Contains(KeyValuePair item) { - throw new NotSupportedException(); + return dict.Contains(item); } bool IDictionary.ContainsKey(string key) { - throw new NotSupportedException(); + return dict.ContainsKey(key); } void ICollection>.CopyTo(KeyValuePair[] array, int arrayIndex) { - throw new NotSupportedException(); + foreach (var item in array) + { + SetValue(item.Key, item.Value); + } } IEnumerator> IEnumerable>.GetEnumerator() { - throw new NotSupportedException(); + return dict.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { - throw new NotSupportedException(); + return dict.GetEnumerator(); } bool IDictionary.Remove(string key) { - throw new NotSupportedException(); + return dict.Remove(key); } bool ICollection>.Remove(KeyValuePair item) { - throw new NotSupportedException(); + return dict.Remove(item.Key); } bool IDictionary.TryGetValue(string key, out object value) { - throw new NotSupportedException(); + return dict.TryGetValue(key, out value); } private class FastDynamicMetaObject : DynamicMetaObject