Skip to content

Instantly share code, notes, and snippets.

@diegodfsd
Created February 13, 2019 17:44
Show Gist options
  • Save diegodfsd/2969d39c27f1fc004163bde259a01f31 to your computer and use it in GitHub Desktop.
Save diegodfsd/2969d39c27f1fc004163bde259a01f31 to your computer and use it in GitHub Desktop.
public sealed class DynamicResource : DynamicObject, IDictionary<string, object>
{
/// <summary>
/// String Dictionary that contains the extra dynamic values
/// stored on this object/instance
/// </summary>
/// <remarks>Using PropertyBag to support XML Serialization of the dictionary</remarks>
private IDictionary<string, object> _properties = new Dictionary<string, object>();
/// <summary>
/// Allows passing in an existing instance variable to 'extend'.
/// </summary>
/// <remarks>
/// You can pass in null here if you don't want to
/// check native properties and only check the Dictionary!
/// </remarks>
/// <param name="instance"></param>
public DynamicResource(object instance)
{
Initialize(instance);
}
private void Initialize(object instance)
{
if (instance != null)
_properties = (IDictionary<string,object>)instance;
}
/// <summary>
/// Try to retrieve a member by name first from instance properties
/// followed by the collection entries.
/// </summary>
/// <param name="binder"></param>
/// <param name="result"></param>
/// <returns></returns>
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = _properties[binder.Name];
return result != null;
}
public override IEnumerable<string> GetDynamicMemberNames()
{
return _properties.Select(c => c.Key);
}
public override string ToString()
{
var sb = new StringBuilder(512);
foreach (var str in GetDynamicMemberNames())
{
sb.AppendFormat("{0}='{1}', ", str, _properties[str]);
}
return sb.ToString(0, sb.Length - 2);
}
public void Add(string key, object value)
{
_properties.Add(key, value);
}
public bool ContainsKey(string key)
{
return _properties.ContainsKey(key);
}
public ICollection<string> Keys => _properties.Keys;
public bool Remove(string key)
{
return _properties.Remove(key);
}
public bool TryGetValue(string key, out object value)
{
return _properties.TryGetValue(key, out value);
}
public ICollection<object> Values => _properties.Values;
public object this[string key]
{
get
{
var exists = _properties.TryGetValue(key, out var result);
if (!exists)
{
Trace.WriteLine("Error getting key: " + key);
throw new KeyNotFoundException("Error getting key: " + key);
}
return result;
}
set => _properties[key] = value;
}
public void Add(KeyValuePair<string, object> item)
{
_properties.Add(item.Key, item.Value);
}
public void Clear()
{
_properties.Clear();
}
public bool Contains(KeyValuePair<string, object> item)
{
return _properties.Contains(item);
}
public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
{
throw new NotImplementedException();
}
public int Count => _properties.Count;
public bool IsReadOnly => true;
public bool Remove(KeyValuePair<string, object> item)
{
return _properties.Remove(item.Key);
}
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
{
return _properties.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return _properties.GetEnumerator();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment