Skip to content

Instantly share code, notes, and snippets.

@aaronpowell
Created December 3, 2013 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronpowell/7763203 to your computer and use it in GitHub Desktop.
Save aaronpowell/7763203 to your computer and use it in GitHub Desktop.
Dynamically creating expression trees
void Main()
{
var pi = typeof(Foo).GetProperty("Bar");
Expression<Func<Foo, string>> x = f => f.Bar;
x.Dump();
var p = Expression.Parameter(pi.DeclaringType, "f");
var lambda = Expression.Lambda(
parameters: new[] { p },
body: Expression.Property(
p,
pi
)
);
lambda.Dump();
var foo = new Foo { Bar = "Baz" };
x.Compile().DynamicInvoke(foo).Dump();
lambda.Compile().DynamicInvoke(foo).Dump();
}
// Define other methods and classes here
class Foo {
public string Bar { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment