Created
June 6, 2014 18:03
-
-
Save rushfrisby/177e06258c3a8b9c0b46 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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