コード例 #1
0
        private static IEnumerable <Expression> GetChildren(MemberBinding binding)
        {
            switch (binding.BindingType)
            {
            case MemberBindingType.Assignment:
                yield return(((MemberAssignment)binding).Expression);

                break;

            case MemberBindingType.MemberBinding:
                foreach (Expression child in ExpressionExtensions.GetChildren((IEnumerable <MemberBinding>)((MemberMemberBinding)binding).Bindings))
                {
                    yield return(child);
                }
                break;

            case MemberBindingType.ListBinding:
                using (IEnumerator <Expression> enumerator = ExpressionExtensions.GetChildren((IEnumerable <ElementInit>)((MemberListBinding)binding).Initializers).GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        Expression child = enumerator.Current;
                        yield return(child);
                    }
                    break;
                }
            }
        }
コード例 #2
0
        public static IEnumerable <Expression> GetChildren(this Expression exp)
        {
            switch (exp)
            {
            case UnaryExpression _:
                yield return(((UnaryExpression)exp).Operand);

                break;

            case BinaryExpression _:
                yield return(((BinaryExpression)exp).Left);

                yield return(((BinaryExpression)exp).Right);

                yield return((Expression)((BinaryExpression)exp).Conversion);

                break;

            case TypeBinaryExpression _:
                yield return(((TypeBinaryExpression)exp).Expression);

                break;

            case ConditionalExpression _:
                yield return(((ConditionalExpression)exp).Test);

                yield return(((ConditionalExpression)exp).IfTrue);

                yield return(((ConditionalExpression)exp).IfFalse);

                break;

            case MemberExpression _:
                yield return(((MemberExpression)exp).Expression);

                break;

            case MethodCallExpression _:
                yield return(((MethodCallExpression)exp).Object);

                foreach (Expression expression in ((MethodCallExpression)exp).Arguments)
                {
                    yield return(expression);
                }
                break;

            case LambdaExpression _:
                yield return(((LambdaExpression)exp).Body);

                foreach (ParameterExpression parameter in ((LambdaExpression)exp).Parameters)
                {
                    yield return((Expression)parameter);
                }
                break;

            case NewExpression _:
                foreach (Expression expression in ((NewExpression)exp).Arguments)
                {
                    yield return(expression);
                }
                break;

            case NewArrayExpression _:
                foreach (Expression expression in ((NewArrayExpression)exp).Expressions)
                {
                    yield return(expression);
                }
                break;

            case InvocationExpression _:
                yield return(((InvocationExpression)exp).Expression);

                foreach (Expression expression in ((InvocationExpression)exp).Arguments)
                {
                    yield return(expression);
                }
                break;

            case MemberInitExpression _:
                yield return((Expression)((MemberInitExpression)exp).NewExpression);

                foreach (Expression child in ExpressionExtensions.GetChildren((IEnumerable <MemberBinding>)((MemberInitExpression)exp).Bindings))
                {
                    yield return(child);
                }
                break;

            case ListInitExpression _:
                yield return((Expression)((ListInitExpression)exp).NewExpression);

                foreach (Expression child in ExpressionExtensions.GetChildren((IEnumerable <ElementInit>)((ListInitExpression)exp).Initializers))
                {
                    yield return(child);
                }
                break;
            }
        }