Created
August 10, 2011 03:16
-
-
Save jrwren/1136018 to your computer and use it in GitHub Desktop.
dynamic
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
public static class DynamicExt | |
{ | |
public static Dictionary<object, DynamicObject> registry = new Dictionary<object, DynamicObject>(); | |
public static dynamic AsDynamic(this object source) | |
{ | |
if (!registry.ContainsKey(source)) | |
registry[source] = new DynamicObject(source); | |
return registry[source]; | |
} | |
} | |
public class DynamicObject : System.Dynamic.IDynamicMetaObjectProvider | |
{ | |
object original; | |
protected System.Dynamic.ExpandoObject expandoObject = new System.Dynamic.ExpandoObject(); | |
public DynamicObject(object original) | |
{ | |
this.original = original; | |
} | |
public System.Dynamic.DynamicMetaObject GetMetaObject(System.Linq.Expressions.Expression parameter) | |
{ | |
return (expandoObject as System.Dynamic.IDynamicMetaObjectProvider).GetMetaObject(parameter); | |
} | |
} | |
[TestFixture] | |
public class DynamicExtTests | |
{ | |
[Test] | |
public void CanOverRideReverse() | |
{ | |
string test = "1234"; | |
test.AsDynamic().Reverse = (Func<string, string>)((s) => "tricked you"); | |
var actual = test.AsDynamic().Reverse(); | |
Assert.AreEqual("tricked you", actual); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment