简体   繁体   中英

How to get full name path for method call/class declaration using Roslyn

How can I get the full qualified name of a method call with Roslyn?

For example, Request.QueryString , comes from System.Web.UI , how am I able to detect that?

How about class declaration within same project but different namespaces? As well as function call from other classes of the same project.

Appreciate any form of help, thanks!

You should create Compilation for all SyntaxTree for your all project files. After it you can use symbol info for any node:

static string Code =
@"namespace TestNamespace
{
    public class Test
    {
        public int A { get; set; }
        public int B { get; set; }

        public Test(int a, int b)
        {
            A = a;
            B = b;
        }
    }
}";

static void Main(string[] args)
{
    var syntaxTree = CSharpSyntaxTree.ParseText(Code);
    var syntaxTrees = new SyntaxTree[] { syntaxTree }; // Add SyntaxTree array from project files.
    var compilation = CSharpCompilation.Create("tempAssembly", syntaxTrees);
    var semanticModel = compilation.GetSemanticModel(syntaxTree);
    var caretPosition = 46;
    var symbol = SymbolFinder.FindSymbolAtPositionAsync(semanticModel, caretPosition, new AdhocWorkspace()).Result;
    var fullName = symbol.ToString(); // fullName is "TestNamespace.Test"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM