Tay*_*ite 4 code-generation roslyn roslyn-code-analysis sourcegenerators
我正在尝试开发一个源生成器,以使用该接口在部分类上自动实现接口。
我相信这一定是Microsoft 新的 Source Generators 的常见用例,甚至在Roslyn Source Generator Cookbook中被列为用例,但没有示例实现。
我进行了搜索,但很难在 Roslyn 分析器中找到针对此场景的问题。
在说明书中,他们使用 SyntaxReceiver 类来过滤调用应处理哪些语法节点Execute:
class SluggableSyntaxReceiver : ISyntaxReceiver
{
public List<ClassDeclarationSyntax> ClassesToAugment { get; } = new List<ClassDeclarationSyntax>();
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
{
// Business logic to decide what we're interested in goes here
if(syntaxNode is ClassDeclarationSyntax cds && cds.HasInterface("IChangeTracked"))
ClassesToAugment.Add(cds)
}
}
Run Code Online (Sandbox Code Playgroud)
查看食谱以了解生成器的实现细节。
我想要确定的是如何HasInterface在 ClassDeclarationSyntax 节点上实现我的扩展。
public static bool HasInterface(this ClassDeclarationSyntax source, string interfaceName)
{
IEnumerable<TypeSyntax> baseTypes = source.BaseList.Types.Select(baseType=>baseType.Type);
// Ideally some call to do something like...
return baseTypes.Any(baseType=>baseType.Name==interfaceName);
}
Run Code Online (Sandbox Code Playgroud)
我如何能:
我必须相信有比我所做的更好的方法来做到这一点,但就其价值而言,这里有一个工作实现,可以确定特定的ClassDeclarationSyntax显式实现接口:
/// <summary>Indicates whether or not the class has a specific interface.</summary>
/// <returns>Whether or not the SyntaxList contains the attribute.</returns>
public static bool HasInterface(this ClassDeclarationSyntax source, string interfaceName)
{
IEnumerable<BaseTypeSyntax> baseTypes = source.BaseList.Types.Select(baseType => baseType);
// To Do - cleaner interface finding.
return baseTypes.Any(baseType => baseType.ToString() ==interfaceName);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2119 次 |
| 最近记录: |