Skip to content

Instantly share code, notes, and snippets.

@mattwarren
Created September 22, 2010 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattwarren/591970 to your computer and use it in GitHub Desktop.
Save mattwarren/591970 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Dynamic;
using System.Linq.Expressions;
using System.Diagnostics;
namespace Raven.Client.Tests.Document
{
//from http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.aspx
public class CustomDynamicClass : DynamicObject, IDictionary<string, object>
{
// The inner dictionary, used to store the new dynamic members and their values
Dictionary<string, object> dictionary = new Dictionary<string, object>();
// This property returns the number of elements in the inner dictionary.
public int Count
{
get { return dictionary.Count; }
}
// If you try to get a value of a property not defined in the class, this method is called.
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
// Converting the property name to lowercase so that property names become case-insensitive.
string name = binder.Name.ToLower();
// If the property name is found in a dictionary, set the result parameter to the
// property value and return true. Otherwise, return false.
return dictionary.TryGetValue(name, out result);
}
// If you try to set a value of a property that is not defined in the class, this method is called.
public override bool TrySetMember(SetMemberBinder binder, object value)
{
// Converting the property name to lowercase so that property names become case-insensitive.
dictionary[binder.Name.ToLower()] = value;
// You can always add a value to a dictionary,
// so this method always returns true.
return true;
}
//See http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.trysetindex.aspx
public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
{
int index = (int)indexes[0];
return dictionary.TryGetValue("Property" + index, out result);
}
public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value)
{
int index = (int)indexes[0];
// If a corresponding property already exists, set the value.
if (dictionary.ContainsKey("Property" + index))
dictionary["Property" + index] = value; // <-- this line needs to be changed in the sample it's dictionary["Property"] = value;
else
// If a corresponding property does not exist, create it.
dictionary.Add("Property" + index, value);
return true;
}
public override IEnumerable<string> GetDynamicMemberNames()
{
return dictionary.Keys;
}
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
{
return dictionary.GetEnumerator();
}
public void Add(string key, object value)
{
throw new NotImplementedException();
}
public bool ContainsKey(string key)
{
throw new NotImplementedException();
}
public ICollection<string> Keys
{
get { throw new NotImplementedException(); }
}
public bool Remove(string key)
{
throw new NotImplementedException();
}
public bool TryGetValue(string key, out object value)
{
throw new NotImplementedException();
}
public ICollection<object> Values
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
public object this[string key]
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public void Add(KeyValuePair<string,object> item)
{
throw new NotImplementedException();
}
public void Clear()
{
throw new NotImplementedException();
}
public bool Contains(KeyValuePair<string,object> item)
{
throw new NotImplementedException();
}
public void CopyTo(KeyValuePair<string,object>[] array, int arrayIndex)
{
throw new NotImplementedException();
}
public bool IsReadOnly
{
get { throw new NotImplementedException(); }
}
public bool Remove(KeyValuePair<string,object> item)
{
throw new NotImplementedException();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment