Skip to content

Instantly share code, notes, and snippets.

@mscottford
Created June 8, 2009 21:23
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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.
// 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