Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 282
Görkem Özer Avatar asked Aug 10 '17 12:08

Görkem Özer


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Why do we write C?

We write C for Carbon Because in some element the symbol of the element is taken form its first words and Co for Cobalt beacause in some elements the symbol of the element is taken from its first second letters, so that the we don't get confuse.


1 Answers

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

        }
    }
}
like image 129
SJP Avatar answered Oct 20 '22 18:10

SJP