Created
July 14, 2012 00:44
-
-
Save hoganlong/3108507 to your computer and use it in GitHub Desktop.
MakeGetter code
This file contains hidden or 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
class test | |
{ | |
public string sam { get; set; } | |
public int anumber { get; set; } | |
} | |
void Main() | |
{ | |
var z = new test { sam = "sam", anumber = 99}; | |
var x1 = MakeGetter<test,int>("anumber"); | |
var x2 = MakeGetter<test,string>("sam"); | |
x1(z).Dump(); | |
x2(z).Dump(); | |
} | |
Func<T, RT> MakeGetter<T,RT>(string propertyName) | |
{ | |
ParameterExpression input = Expression.Parameter(typeof(T)); | |
var expr = Expression.Property(input, typeof(T).GetProperty(propertyName)); | |
return Expression.Lambda<Func<T, RT>>(expr, input).Compile(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results
99
sam