问题
How to get file name and position of definition of any symbol under the current custom position and file (within project or solution)?
I do the following steps (simplified explanation):
Create collection of syntax trees and compilation by the following way:
SyntaxTrees = new List<SyntaxTree>(); foreach (var file in projectFiles) syntaxTrees.Add(SyntaxTree.ParseText(File.ReadAllText(file)); Compilation = Compilation.Create("temp.cs", null, SyntaxTrees, new MetadataReference[] { mscorlib });
Get current token in syntax tree:
var token = currentTree.GetRoot().FindToken(textPos, false);
Symbol have appropriate method for definition getting: DeclaringSyntaxNodes
.
How can I resolve token
on Symbol
in SemanticModel
? Thanks.
回答1:
You're looking for the SymbolFinder.FindSymbolAtPosition method.
来源:https://stackoverflow.com/questions/27259600/go-to-definition-from-text-position-with-roslyn