コード例 #1
0
        /// <summary>
        /// Converts the specified expression into an enumerable of expressions by walking the expression tree
        /// </summary>
        /// <param name="expression">The expression to enumerate</param>
        /// <returns>An enumerable containing all expressions in the input expression</returns>
        public static IEnumerable <Expression> AsEnumerable(this Expression expression)
        {
            var walker = new ExpressionWalker();

            walker.Visit(expression);
            return(walker.Expressions);
        }
            private EvaluationResult AnalyzeExpression([NotNull] IOperation expression)
            {
                Guard.NotNull(expression, nameof(expression));

                context.CancellationToken.ThrowIfCancellationRequested();

                var walker = new ExpressionWalker(this);

                walker.Visit(expression);

                return(walker.Result);
            }
コード例 #3
0
ファイル: FAQ.cs プロジェクト: Rickinio/roslyn
        public void TraverseAllExpressionsInASyntaxTreeUsingAWalker()
        {
            var tree = SyntaxFactory.ParseSyntaxTree(@"
using System;
class Program
{
    static void Main()
    {
        var i = 0.0;
        i += 1 + 2L;
    }
}");
            var compilation = CSharpCompilation.Create("MyCompilation")
                .AddReferences(Mscorlib)
                .AddSyntaxTrees(tree);
            var model = compilation.GetSemanticModel(tree);
            var walker = new ExpressionWalker() { SemanticModel = model };

            walker.Visit(tree.GetRoot());
            Assert.Equal(@"
PredefinedTypeSyntax void has type void
IdentifierNameSyntax var has type double
LiteralExpressionSyntax 0.0 has type double
AssignmentExpressionSyntax i += 1 + 2L has type double
IdentifierNameSyntax i has type double
BinaryExpressionSyntax 1 + 2L has type long
LiteralExpressionSyntax 1 has type int
LiteralExpressionSyntax 2L has type long", walker.Results.ToString());
        }