Skip to content

Instantly share code, notes, and snippets.

@sandcastle
Last active January 4, 2016 19:29
Show Gist options
  • Save sandcastle/8667260 to your computer and use it in GitHub Desktop.
Save sandcastle/8667260 to your computer and use it in GitHub Desktop.
a case in-sensitive dynamic object.
public class CaseInsensitiveExpando : DynamicObject, IDictionary<string, object>
{
readonly IDictionary<string, object> _dictionary = new Dictionary<string, object>(StringComparer.InvariantCultureIgnoreCase);
public int Count
{
get { return this._dictionary.Keys.Count; }
}
public bool IsReadOnly
{
get { return _dictionary.IsReadOnly; }
}
public void Add(KeyValuePair<string, object> item)
{
_dictionary.Add(item);
}
public void Clear()
{
_dictionary.Clear();
}
public bool Contains(KeyValuePair<string, object> item)
{
return _dictionary.Contains(item);
}
public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
{
_dictionary.CopyTo(array, arrayIndex);
}
public bool Remove(KeyValuePair<string, object> item)
{
return _dictionary.Remove(item);
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (this._dictionary.ContainsKey(binder.Name))
{
result = this._dictionary[binder.Name];
return true;
}
return base.TryGetMember(binder, out result);
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
if (!this._dictionary.ContainsKey(binder.Name))
this._dictionary.Add(binder.Name, value);
else
this._dictionary[binder.Name] = value;
return true;
}
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
if (this._dictionary.ContainsKey(binder.Name) && this._dictionary[binder.Name] is Delegate)
{
Delegate del = this._dictionary[binder.Name] as Delegate;
result = del.DynamicInvoke(args);
return true;
}
return base.TryInvokeMember(binder, args, out result);
}
public override bool TryDeleteMember(DeleteMemberBinder binder)
{
if (this._dictionary.ContainsKey(binder.Name))
{
this._dictionary.Remove(binder.Name);
return true;
}
return base.TryDeleteMember(binder);
}
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
{
return _dictionary.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public bool ContainsKey(string key)
{
return _dictionary.ContainsKey(key);
}
public void Add(string key, object value)
{
_dictionary.Add(key, value);
}
public bool Remove(string key)
{
return _dictionary.Remove(key);
}
public bool TryGetValue(string key, out object value)
{
return _dictionary.TryGetValue(key, out value);
}
public object this[string key]
{
get { return _dictionary[key]; }
set { _dictionary[key] = value; }
}
public ICollection<string> Keys
{
get { return _dictionary.Keys; }
}
public ICollection<object> Values
{
get { return _dictionary.Values; }
}
}
void Main()
{
dynamic x = new CaseInsensitiveExpando();
x.Name = "test";
// Should return true for both
Console.WriteLine(x.name == "test");
Console.WriteLine(x.Name == "test");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment