Skip to content

Instantly share code, notes, and snippets.

@rushfrisby
Created June 6, 2014 18:03
Show Gist options
  • Save rushfrisby/177e06258c3a8b9c0b46 to your computer and use it in GitHub Desktop.
Save rushfrisby/177e06258c3a8b9c0b46 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Dynamic;
public class TurboObject : DynamicObject
{
private readonly object _defaultValue;
private readonly Dictionary<string, object> _vals = new Dictionary<string, object>();
public TurboObject()
{
}
public TurboObject(object defaultValue)
{
_defaultValue = defaultValue;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = _defaultValue;
if (_vals.ContainsKey(binder.Name))
result = _vals[binder.Name];
return true;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
if (!_vals.ContainsKey(binder.Name))
_vals.Add(binder.Name, value);
else
_vals[binder.Name] = value;
return true;
}
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
var del = (Delegate)new Action(() => { });
if (_vals.ContainsKey(binder.Name))
del = (Delegate)_vals[binder.Name];
result = del.DynamicInvoke(args);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment