我正在尝试实现一个ICodeIssueProvider
来检测一个类(或其基类型之一)是否具有某个属性。
public IEnumerable<CodeIssue> GetIssues(IDocument document,
CommonSyntaxNode node,
CancellationToken cancellationToken)
{
var methodDeclaration = (MethodDeclarationSyntax)node;
var semanticModel = document.GetSemanticModel(cancellationToken);
var methodSymbol = semanticModel.GetDeclaredSymbol(methodDeclaration);
var typeSymbol = methodSymbol.ContainingType;
// The following only gets attributes declared on this class, how to
// also include those declared on a base class ?
var attributes = typeSymbol.GetAttributes();
有没有比一路typeSymbol.BaseType
走上来System.Object
,在路上呼唤更好GetAttributes()
的方法呢?
此外,是否有更好的方法来检查 a 是否typeSymbol
来自特定类而不是.BaseType
手动检查
(是的,从下面的示例中检查MethodDeclarationSyntax
节点而不是ClassDeclarationSyntax
节点的原因并不明显)