Created
December 3, 2013 02:55
-
-
Save aaronpowell/7763203 to your computer and use it in GitHub Desktop.
Dynamically creating expression trees
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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