Search code examples
c#roslynroslyn-code-analysis

C# Roslyn Compiler - How to get namespace of a type from IdentifierNameSyntax?


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


Solution

  • 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>()
            {
    
            }
        }
    }