8

Suppose I have a call in the code, SomeClass.SomeStaticMethod<T>(), which is an InvocationExpressionSyntax.

I get name of the generic type T as string (from IdentifierNameSyntax). I tried to get Symbol of T but I did not succeed.

How can I find out the namespace information of type T?

UPDATE: @SJP's answer is correct. I want to explain my mistake for those who want to get namespace information from IdentifierNameSyntax, which contains an identifier for a class (class name):

My initial aim was to find invocations in SomeClass.SomeMethod<T>() format and get namespace information of type T.

var namedTypeSymbol = context.Symbol as INamedTypeSymbol;
var reference = nameTypeSymbol.DeclaringSyntaxReferences.First();
var classSyntaxTree = reference.SyntaxTree;

var semanticModel = context.Compilation.GetSemanticModel(classSyntaxTree);
var genericNameSyntax = (GenericNameSyntax)((MemberAccessExpressionSyntax)node.Expression).Name;
var identifierNameSyntax = genericNameSyntax.TypeArgumentList.Arguments.First();
var typeInfo = semanticModel.GetTypeInfo(identifierNameSyntax);
var nameSpace = ((INamedTypeSymbol)typeInfo.Type).ContainingNamespace;
var nameSpaceName = nameSpace.ToString();

Here is my mistake:

I tried to get full namespace like <module_name>.<namespace_part_1>.<namespace_part_2> but when I did namedTypeSymbol.ContainingNamespace.Name , I got only <namespace_part_2>. After couple of hours, I found that getting full namespace is done like namedTypeSymbol.ContainingNamespace.ToString().

Sometimes best thing to do is going outside and taking fresh air :)

Görkem Özer
  • 504
  • 5
  • 13

3 Answers3

8

You're going to need the semantic model to achieve your task. Assuming you require the Namespace of SomeClass, you can then just obtain the type and name from there the namespace from the MemberAccessExpressionSyntax by accessing the Name Field of the Expression as follows:

var semanticModel = await document.GetSemanticModelAsync()
var name = (GenericNameSyntax)((MemberAccessExpressionSyntax)node.Expression).Name;
var typeInfo = semanticModel.GetTypeInfo(name.TypeArgumentList.Arguments.First());
var nameSpace = ((INamedTypeSymbol)typeInfo.Type).ContainingNamespace;
var nameSpaceName = nameSpace.Name;

For the following example Program this would result in "System" or "ConsoleApp1" (depending on the call) for the variable nameSpaceName while all other information can be accessed by the variable nameSpace.

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Program.DoStuff<string>();
            Program.DoStuff<Program>();
        }

        static void DoStuff<T>()
        {

        }
    }
}
SJP
  • 1,000
  • 1
  • 10
  • 19
  • 2
    Yes, OP needs the semantic model. But he needs GetSymbolInfo on the invocation itself, and then from the IMethodSymbol the first type parameter. I assume the question was not clear before due to bad formatting, but I fixed that now. – Kris Vandermotten Aug 10 '17 at 15:32
  • Much obliged, I didn't recognize the original intention of the question asked. I adjusted the code accordingly. – SJP Aug 10 '17 at 15:47
  • @SJP Nice way better now – johnny 5 Aug 10 '17 at 15:47
  • Firstly, thanks for the correction @KrisVandermotten, the question was not clear, I realize now. Also, thanks for the detailed, well-explained answer. However I still have trouble with getting namespace name. I use SyntaxNodeAnalysisContext as context, and when I do context.SemanticModel.GetTypeInfo(identifierNameSyntax), I can only get generic type name. When I try to get (INamedTypeSymbol)typeInfo.Type).ContainingNamespace.Name, I get "Interface" instead of ".". – Görkem Özer Aug 11 '17 at 11:16
  • I think I couldn't get SemanticModel correctly. I couldn't get "document" as you did, since I'm writing code for analyzer. My aim is to capture invocations in "SomeClass.SomeStaticMethod()" format and give warning when T is not in the same namespace with the class doing invocation. I hope I explained myself better. Thanks for your help in advance. – Görkem Özer Aug 11 '17 at 11:22
  • If you're working in a DiagnosticAnalyzer you should have a SymbolAnalysisContext at your hand which can provide you the Compilation which also allows to attain the semantic model. Otherwise the exact context could prove useful. – SJP Aug 11 '17 at 11:41
1

The ToDisplayString method lets you pass in a format object which has a number of options for controlling how you want to format stuff.

Passing SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces will ensure that both the Namespace + Class name are returned.

var symbolDisplayFormat = new SymbolDisplayFormat(typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces);

string fullyQualifiedName = typeSymbol.ToDisplayString(symbolDisplayFormat);
Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
0

A call to Function{SuppliedArgument}(...) is gonna have a GenericNameSyntax as the {SuppliedArgument}. Which isnt the same as a TypeDeclaration.TypeParameterList.Parameters. You can't get anything from those. Theyre placeholders. You need to go through the GenericNameSyntax.TypeArgumentList, and use SemanticModel.GetSymbolInfo(identifierNameSyntax).Symbol.ContainingNamespace on each to get the namespaces the identifiers are contained in.