public static List <Expression> Search <TExpression>(Expression expression) where TExpression : Expression { var searcher = new ExpressionSearcher(e => e is TExpression); searcher.Visit(expression); return(searcher.nodes.ToList()); }
public static List <Expression> Search(Expression expression, Func <Expression, bool> criteria, Func <Expression, bool> parentExclusionCriteria = null) { var searcher = new ExpressionSearcher(criteria, parentExclusionCriteria); searcher.Visit(expression); return(searcher.nodes.ToList()); }
private bool GetDirection(BinaryExpression binary) { if (ExpressionSearcher.Search <PropertyExpression>(binary.Left).Count > 0) { return(true); } return(false); }
private bool DifferentEnumTypesInternal(Expression node, Expression first, Expression second, out Expression outExpr) { var firstUnwrapped = SubstituteExpression.Strip(ExpressionHelpers.UnwrapCast(first)); var secondUnwrapped = SubstituteExpression.Strip(ExpressionHelpers.UnwrapCast(second)); if (IsEnum(firstUnwrapped) && IsEnum(secondUnwrapped)) { var property = ExpressionSearcher.Search(firstUnwrapped, p => p is PropertyExpression).FirstOrDefault(); if (property != null) { var comparison = ExpressionSearcher.Search(secondUnwrapped, e => IsEnum(e) && e.NodeType != ExpressionType.Convert && e.NodeType != ExpressionType.ConvertChecked); var firstComparison = comparison.FirstOrDefault(); if (firstComparison != null) { if (firstComparison.Type.IsEnum && firstComparison.Type != property.Type) { if (strict) { throw Error.InvalidEnumComparison(node, firstUnwrapped, secondUnwrapped); } Logger.Log($"Condition {node} compares enums of different types. Reducing to 'True'", Indentation.Six); outExpr = Expression.Constant(true); return(true); } else { var constant = firstComparison as ConstantExpression; if (constant != null) { if (property.Type != constant.Value.GetType()) { Logger.Log($"Condition {node} compares enums of different types. Reducing to 'True'", Indentation.Six); if (strict) { throw Error.InvalidEnumComparison(node, firstUnwrapped, secondUnwrapped); } outExpr = Expression.Constant(true); return(true); } } } } } } outExpr = null; return(false); }
private bool IsLiteralMemberInitExpression(MemberInitExpression init, Expression source) { //That is to say, we are not the MemberInitExpression defined in method CreateSourceExpression() if (init.Type != typeof(T)) { return(true); } //We ARE a MemberInitExpression of type T, but are we a REAL PropertyExpression with a backing MemberExpression, //or a fake one used to perform property lookups. if (ExpressionSearcher.Search(source, e => e is PropertyExpression).Cast <PropertyExpression>() .Any(p => p.Expression != null)) { //We're a MemberInitExpression for type T. If we did new Sensor { Message = <root>.Name }.Message, //continuing with normal MemberInitExpression parsing logic would reduce us to <root>.Message, //eliminating the reference to <root>.Name. As such, we will need to perform a lookup on the //assignment "source", which will allow us to determine the whole thing can in fact be replaced //with <root>.Name return(true); } return(false); }
private Expression PropertyExpressionFromArray(MemberExpression node, Expression source) { var exprs = ExpressionSearcher.Search <NewExpression>(source); Expression expr; if (exprs.Cast <NewExpression>().All(e => e.Members == null)) { var memberInit = ExpressionSearcher.Search <MemberInitExpression>(source); if (memberInit.Count > 0) { exprs = memberInit; } foreach (var e in exprs) { if (PropertyExpressionFromInit(node, e, out expr)) { return(expr); } } } else { foreach (var e in exprs) { if (PropertyExpressionFromPrevious(node, e, out expr)) { return(expr); } } } throw new InvalidOperationException($"Failed to find member '{node}' in source expression array. This should be impossible"); }
private bool IsLegalCondition(Expression node) { //We DON'T use IsBinaryCondition here, since we want to know whether we might have something illegal //like ((s.Id + 1) == 3) return(ExpressionSearcher.Search(node, e => (e is BinaryExpression & e.NodeType != ExpressionType.ArrayIndex) || IsMethodCondition(e)).Count > 0); }