Instantly share code, notes, and snippets.

Embed
What would you like to do?
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