Created
September 19, 2012 15:02
-
-
Save takeshik/3750141 to your computer and use it in GitHub Desktop.
複数のラムダ式から式木を構築する ref: http://qiita.com/items/438b845154c6c21fcba5
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
Expression<Func<int, int, bool>> expr1 = (x, y) => x * y < 20; | |
Expression<Func<int, int, bool>> expr2 = (x, y) => x + y == 8; | |
Expression<Func<int, int, bool>> expr = CombineLambda(expr1, expr2); | |
Console.WriteLine(expr.Compile()(3, 5)); // True | |
public static Expression<TDelegate> CombineLambda<TDelegate>(Expression<TDelegate> left, Expression<TDelegate> right) | |
{ | |
// TODO: ここを埋めたい! | |
throw new NotImplementedException(); | |
} |
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 static Expression<TDelegate> CombineLambda<TDelegate>(Expression<TDelegate> left, Expression<TDelegate> right) | |
{ | |
return Expression.AndAlso(left, right); | |
} |
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 static Expression<TDelegate> CombineLambda<TDelegate>(Expression<TDelegate> left, Expression<TDelegate> right) | |
{ | |
return Expression.Lambda<TDelegate>(Expression.AndAlso(left, right)); | |
} |
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 static Expression<TDelegate> CombineLambda<TDelegate>(Expression<TDelegate> left, Expression<TDelegate> right) | |
{ | |
return Expression.Lambda<TDelegate>(Expression.AndAlso(left, right)); | |
} |
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 static Expression<TDelegate> CombineLambda<TDelegate>(Expression<TDelegate> left, Expression<TDelegate> right) | |
{ | |
return Expression.Lambda<TDelegate>(Expression.AndAlso(left.Body, right.Body), left.Parameters); | |
} |
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 static Expression<TDelegate> CombineLambda<TDelegate>(Expression<TDelegate> left, Expression<TDelegate> right) | |
{ | |
var visitor = new ParameterVisitor(left.Parameters); | |
return Expression.Lambda<TDelegate>(Expression.AndAlso(left.Body, visitor.Visit(right.Body)), visitor.Parameters); | |
} |
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 ParameterVisitor | |
: ExpressionVisitor | |
{ | |
private readonly IDictionary<Tuple<Type, string>, ParameterExpression> _parameters; | |
public ICollection<ParameterExpression> Parameters | |
{ | |
get | |
{ | |
return this._parameters.Values; | |
} | |
} | |
public ParameterVisitor(IEnumerable<ParameterExpression> parameters) | |
{ | |
this._parameters = parameters.ToDictionary(p => Tuple.Create(p.Type, p.Name)); | |
} | |
protected override Expression VisitParameter(ParameterExpression node) | |
{ | |
var key = Tuple.Create(node.Type, node.Name); | |
return this._parameters.ContainsKey(key) | |
? this._parameters[key] | |
: node; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment