简体   繁体   中英

VisitClassDeclaration node.Identifier in Roslyn

如何在声明所在的源文件中找到哪个行号?

Disclaimer: I work for Microsoft on the Roslyn team.

You can use the ISyntaxTree.GetLineSpan() method to convert to a line number. For example, given an ISymbol "symbol", you can get the start location of the first definition with:

var loc = symbol.Locations.First();
var lineSpan = loc.SourceTree.GetLineSpan(loc.SourceSpan, 
    usePreprocessorDirectives: false);
var line = lineSpan.StartLinePosition.Line;
var character = lineSpan.StartLinePosition.Character;

From the title, it looks like you're starting with a SyntaxNode , so you can just use the Span property directly.

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