Created
July 22, 2010 21:03
-
-
Save atheken/486591 to your computer and use it in GitHub Desktop.
Using Expression trees to Curry a Lambda
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
var func = WrapLambda<Outie, Innie, bool>(j => j.BoolVal, "Inner").Compile(); | |
var resultTrue = func(new Outie { Inner = new Innie { BoolVal = true } }); | |
var resultFalse = func(new Outie { Inner = new Innie() }); |
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
public class Outie | |
{ | |
public Innie Inner { get; set; } | |
} | |
public class Innie | |
{ | |
public bool BoolVal { get; set; } | |
} |
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
public Expression<Func<TOuter, TResult>> WrapLambda<TOuter, TInner, TResult>(Expression<Func<TInner, TResult>> innerExpression, String memberName) | |
{ | |
var oParam = Expression.Parameter (typeof(TOuter)); | |
var oAccess = Expression.MakeMemberAccess (oParam, typeof(TOuter).GetMember (memberName)[0]); | |
return Expression.Lambda<Func<TOuter, TResult>> (Expression.Invoke (innerExpression, oAccess), oParam); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment