Pergunta With block and MemberAccessExpressionSyntax

  • Monday, August 27, 2012 12:41 PM
     
      Has Code
    string code =
                    @"Public Class Sample
                        Public Sub Foo()
                            Dim tb = New System.Web.UI.WebControls.TextBox()
                            With tb
                                .Text = ""
                            End With
                        End Sub
                    End Class";
    
                var tree = SyntaxTree.ParseCompilationUnit(code);
                var compilation = Compilation.Create("Test")
                                 .AddReferences(new Roslyn.Compilers.AssemblyNameReference("mscorlib"))
                                 .AddSyntaxTrees(tree);
                var model = compilation.GetSemanticModel(tree);
    
                var invocations = tree.GetRoot().DescendantNodes().OfType<MemberAccessExpressionSyntax>();
                foreach (var invocation in invocations)
                {
                    var info = model.GetSymbolInfo(invocation).Symbol;
                }

    model.GetSymbolInfo(invocation).Symbol returns null. is it possible to find a type which member is being accessed within a With block?

All Replies

  • Tuesday, August 28, 2012 6:26 PM
     
      Has Code

    I believe the reason for getting null is that your compilation is missing a reference to "System.Web".

    Try adding

    new Roslyn.Compilers.AssemblyNameReference("System.Web")) 

    to the compilation and you should get a property symbol instead of null.

    Thanks,
    Mohammed

  • Tuesday, August 28, 2012 9:56 PM
     
     

    no, it doesn't work. i have updated my example to make it more straightforward

    http://oi46.tinypic.com/hv1ojm.jpg


    • Edited by IvanDunaev Tuesday, August 28, 2012 9:58 PM
    • Edited by IvanDunaev Tuesday, August 28, 2012 9:58 PM
    • Edited by IvanDunaev Tuesday, August 28, 2012 9:59 PM
    •