Skip to content

Instantly share code, notes, and snippets.

@ronnieoverby
Created February 17, 2013 01:14
Show Gist options
  • Select an option

  • Save ronnieoverby/4969553 to your computer and use it in GitHub Desktop.

Select an option

Save ronnieoverby/4969553 to your computer and use it in GitHub Desktop.
private static Func<T, object> CreatePropertyGetter(PropertyInfo propertyInfo)
{
if (typeof(T) != propertyInfo.DeclaringType)
throw new ArgumentException();
var instance = Expression.Parameter(propertyInfo.DeclaringType, "i");
var property = Expression.Property(instance, propertyInfo);
var convert = Expression.TypeAs(property, typeof(object));
return (Func<T, object>)Expression.Lambda(convert, instance).Compile();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment