(请注意:这与运行时反射/元信息无关)
我正在编写 Roslyn CSharpSyntaxVisitor 的具体实现
实施 VisitIdentifierName 时
public override SyntaxNode VisitIdentifierName(IdentifierNameSyntax name)
{
var symbolInfo = _semanticModel.GetSymbolInfo(name);
var fieldSymbol = symbolInfo.Symbol as IFieldSymbol;
if (fieldSymbol != null)
{
// Here I would like to get all the local variable names what are visible
// in the very same scope where this field IdentifierNameSyntax under visiting resides
// We can suppose that _semanticNodel for the Document is available.
}
}
Call SemanticModel.LookupSymbols() https://learn.microsoft.com/en-us/dotnet/api/microsoft.codeanalysis.semanticmodel.lookupsymbols?view=roslyn-dotnet#Microsoft_CodeAnalysis_SemanticModel_LookupSymbols_System_Int32_Microsoft_CodeAnalysis_INamespaceOrTypeSymbol_System_String_System_Boolean_ (source https://github.com/dotnet/roslyn/blob/1331a9d29a90798b0e7338e2829d7cd314960877/src/Compilers/Core/Portable/Compilation/SemanticModel.cs#L458),然后过滤局部变量。
您可能还想过滤掉在该位置之后声明的当地人;看这段代码 https://github.com/dotnet/roslyn/blob/1331a9d29a90798b0e7338e2829d7cd314960877/src/Workspaces/CSharp/Portable/Recommendations/CSharpRecommendationServiceRunner.cs#L239-L242.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)