Created
June 8, 2009 21:23
-
-
Save mscottford/126074 to your computer and use it in GitHub Desktop.
Object helper method that makes it quite a bit easier to work with anonymous classes.
This file contains hidden or 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
// Example | |
// var sample = new { booleanValue = true, stringValue = "Test Value" } | |
// sample.Property<bool>("booleanValue") will result in true | |
// sample.Property<string>("stringValue") will result in "Test Value" | |
static class ObjectExtensions | |
{ | |
public static T Property<T>(this object target, string name) | |
{ | |
return (T)target.GetType().InvokeMember(name, BindingFlags.GetProperty, null, target, new object[] { }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment