Skip to content

Instantly share code, notes, and snippets.

@paavohuhtala
Created September 29, 2014 21:55
Show Gist options
  • Save paavohuhtala/b20b2e8c422501a680f5 to your computer and use it in GitHub Desktop.
Save paavohuhtala/b20b2e8c422501a680f5 to your computer and use it in GitHub Desktop.
it's a lot shorter though
static Delegate WrapGetter(PropertyInfo prop)
{
var handle = Expression.Parameter(typeof (GCHandle), "handle");
var boxedTarget = Expression.Property(handle, "Target");
var unboxedTarget = Expression.Convert(boxedTarget, prop.DeclaringType);
var targetValue = Expression.Property(unboxedTarget, prop);
var wrapperBase = typeof(Func<,>);
var wrapperDelegate = wrapperBase.MakeGenericType(typeof(GCHandle), prop.PropertyType);
return Expression.Lambda(wrapperDelegate, targetValue, handle).Compile();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment