SemanticModel.GetTypeInfo() for ObjectCreationExpressionSyntax.Type returns null

后端 未结 1 1484
孤城傲影
孤城傲影 2021-01-05 15:16

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):

相关标签:
1条回答
  • 2021-01-05 15:46

    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.
    0 讨论(0)
提交回复
热议问题