Skip to content

Commit

Permalink
Implement the rest of the IDictionary<string, object> methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshClose committed Apr 26, 2024
1 parent 249efdf commit 83d3951
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions src/CsvHelper/FastDynamicObject.cs
Original file line number Diff line number Diff line change
@@ -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<string, object>, IDynamicMetaObjectProvider
internal class FastDynamicObject : IDynamicMetaObjectProvider, IDictionary<string, object>
{
private readonly Dictionary<string, object> dict;

Expand All @@ -30,24 +32,24 @@ public FastDynamicObject()

set
{
dict[key] = value;
SetValue(key, value);
}
}

object SetValue(string name, object value)
{
dict[name] = value;
ICollection<string> IDictionary<string, object>.Keys => dict.Keys;

return value;
}
ICollection<object> IDictionary<string, object>.Values => dict.Values;

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

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

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

bool ICollection<KeyValuePair<string, object>>.IsReadOnly => throw new NotSupportedException();
return value;
}

DynamicMetaObject IDynamicMetaObjectProvider.GetMetaObject(Expression parameter)
{
Expand All @@ -56,57 +58,60 @@ DynamicMetaObject IDynamicMetaObjectProvider.GetMetaObject(Expression parameter)

void IDictionary<string, object>.Add(string key, object value)
{
throw new NotSupportedException();
SetValue(key, value);
}

void ICollection<KeyValuePair<string, object>>.Add(KeyValuePair<string, object> item)
{
throw new NotSupportedException();
SetValue(item.Key, item.Value);
}

void ICollection<KeyValuePair<string, object>>.Clear()
{
throw new NotSupportedException();
dict.Clear();
}

bool ICollection<KeyValuePair<string, object>>.Contains(KeyValuePair<string, object> item)
{
throw new NotSupportedException();
return dict.Contains(item);
}

bool IDictionary<string, object>.ContainsKey(string key)
{
throw new NotSupportedException();
return dict.ContainsKey(key);
}

void ICollection<KeyValuePair<string, object>>.CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
{
throw new NotSupportedException();
foreach (var item in array)
{
SetValue(item.Key, item.Value);

This comment has been minimized.

Copy link
@snaumenko-st

snaumenko-st Apr 26, 2024

Contributor

This should copy to an array but not from it

This comment has been minimized.

Copy link
@JoshClose

JoshClose Apr 26, 2024

Author Owner

Good catch.

This comment has been minimized.

Copy link
@JoshClose

JoshClose Apr 26, 2024

Author Owner

Should be fixed now.

}
}

IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()
{
throw new NotSupportedException();
return dict.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
throw new NotSupportedException();
return dict.GetEnumerator();
}

bool IDictionary<string, object>.Remove(string key)
{
throw new NotSupportedException();
return dict.Remove(key);
}

bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> item)
{
throw new NotSupportedException();
return dict.Remove(item.Key);
}

bool IDictionary<string, object>.TryGetValue(string key, out object value)
{
throw new NotSupportedException();
return dict.TryGetValue(key, out value);
}

private class FastDynamicMetaObject : DynamicMetaObject
Expand Down

0 comments on commit 83d3951

Please sign in to comment.