Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SemanticModel.GetTypeInfo() for ObjectCreationExpressionSyntax.Type returns null

Tags:

c#

roslyn

I am trying to get type info from ObjectCreationExpressionSyntax object but failed.

Here is example that reproduce the problem (see "ti.Type is null" in code):

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.MSBuild;

namespace RoslynExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string solutionPath = @"..\..\..\RoslynExample.sln";
            MSBuildWorkspace workspace = MSBuildWorkspace.Create();
            Solution solution = workspace.OpenSolutionAsync(solutionPath).Result;

            foreach (var project in solution.Projects)
            {
                if (project.Name == "RoslynBugProject")
                {
                    foreach (var document in project.Documents)
                    {
                        var compilationUnit = document.GetSyntaxRootAsync().Result;
                        var semanticModel = document.GetSemanticModelAsync().Result;
                        new MyWalker(semanticModel).Visit(compilationUnit);
                    }
                }
            }
        }
    }

    partial class MyWalker : CSharpSyntaxWalker
    {
        private SemanticModel _semanticModel;
        public MyWalker(SemanticModel semanticModel)
        {
            _semanticModel = semanticModel;
        }

        public override void VisitObjectCreationExpression(ObjectCreationExpressionSyntax node)
        {
            var ti = _semanticModel.GetTypeInfo(node.Type); **<--- ti.Type is null**
            base.VisitObjectCreationExpression(node);
        }
    }
}

"RoslynBugProject" project:

namespace RoslynBugProject
{
    internal class Query
    {
    }

    class Program
    {
        static void Main(string[] args)
        {
            var query = new Query();
        }
    }
}

How to get type info? Previous version of Roslyn returns not null value.

like image 690
Alex Sedow Avatar asked Dec 17 '14 21:12

Alex Sedow


1 Answers

You can install the Roslyn Syntax Visualizer, which will show you the syntax tree and also let you explore the SemanticModel APIs as well.

With that installed, you can try right-clicking on nodes and asking for the type symbol:

enter image description here

In this case, you'll discover a couple things:

  • If using "View TypeSymbol (if any)", you can get the symbol from the ObjectCreationExpression itself, but not from any of its children (the "IdentifierName" or the "ArgumentList"), so this could be fixed by passing node to GetTypeInfo instead of node.Type.
  • If using "View Symbol (if any)", you can get the symbol from the "IdentifierName" but not from the ObjectCreationExpression, so you could also fix your code by passing node.Type to GetSymbolInfo instead of GetTypeInfo.
like image 54
David Poeschl Avatar answered Nov 15 '22 00:11

David Poeschl