我正在尝试使用Roslyn来确定项目公开的API(然后使用这些信息做一些进一步的处理,所以我不能只使用反射).我正在使用SyntaxWalker访问声明语法节点,并为每个节点调用IModel.GetDeclaredSymbol.这似乎适用于方法,属性和类型,但它似乎不适用于字段.我的问题是,如何为FieldDeclarationSyntax节点获取FieldSymbol?
这是我正在使用的代码:
public override void VisitFieldDeclaration(FieldDeclarationSyntax node)
{
var model = this._compilation.GetSemanticModel(node.SyntaxTree);
var symbol = model.GetDeclaredSymbol(node);
if (symbol != null
&& symbol.CanBeReferencedByName
// this is my own helper: it just traverses the publ
&& symbol.IsExternallyPublic())
{
this._gatherer.RegisterPublicDeclaration(node, symbol);
}
base.VisitFieldDeclaration(node);
}
Run Code Online (Sandbox Code Playgroud)
Jon*_*eet 28
我来过几次:)
您需要记住,字段声明语法可以声明多个字段.所以你要:
foreach (var variable in node.Declaration.Variables)
{
var fieldSymbol = model.GetDeclaredSymbol(variable);
// Do stuff with the symbol here
}
Run Code Online (Sandbox Code Playgroud)