IIncrementalGenerator - 获取属性类型的命名空间

IIncrementalGenerator - Get namespace of property type

提问人:Dani 提问时间:10/31/2023 更新时间:11/3/2023 访问量:33

问:

根据我的属性列表,我想生成新代码。为此,我还需要添加相应的用途。

如何获取我的属性类型的命名空间? 在这种情况下,prop.Type.ToString()

private static void Execute(Compilation item1, ImmutableArray<(ClassDeclarationSyntax classDeclaration, AttributeData attribute)> classes, SourceProductionContext context)
{
    foreach (var (classDeclaration, attribute) in classes)
    {
         var properties = classDeclaration.Members.AsEnumerable().Where(o => o.IsKind(SyntaxKind.PropertyDeclaration));
         var props = properties.Select(o => o.ChildTokens().Single(n => n.IsKind(SyntaxKind.IdentifierToken)).ValueText).ToArray();

         var snapshotProperties = new List<SnapshotProperty>();
         foreach (var property in properties)
         {
             var prop = (PropertyDeclarationSyntax)property;

             snapshotProperties.Add(new SnapshotProperty(
                 prop.ToString(),
                 prop.Type.ToString(),
                 prop.Identifier.ValueText,
                 prop.Identifier.ValueText.ToLower()));
         }
    }
}

private class SnapshotProperty
{
    public string FullProperty { get; }
    public string Type { get; }
    public string PropertyName { get; }
    public string ParameterName { get; }

    public SnapshotProperty(string fullProperty, string type, string propertyName, string parameterName)
    {
        FullProperty = fullProperty;
        Type = type;
        PropertyName = propertyName;
        ParameterName = parameterName;
    }
}
C# c夏普源生成器

评论


答:

0赞 nalka 11/3/2023 #1

可以在 SemanticModel 上使用 GetTypeInfo 扩展方法,从这里可以获取

  • 键入的名称.Type.Name
  • 包含命名空间的名称 (如果您有多个命名空间,则可能需要遍历 ContainingNamesapce 的 ContainingNamespace,例如.Type.ContainingNamespace.NameNamespace1.Namespace2)
  • 类型的全名 via(某些别名类型将与其别名一起显示,例如 usage on 将显示,有关详细信息,请参阅 SpecialType 属性).Type.ToDisplayString()System.Int32int

如果在调用 的代码中还没有一个,则可以使用 GetSemanticModel : 获取带有 (类型参数) 的 SemanticModel。Executeitem1Compilationitem1.GetSemanticModel(classDeclaration.GetLocation().SourceTree)

假设您想要该类型的全名,则 execute 方法将如下所示:

private static void Execute(Compilation item1, ImmutableArray<(ClassDeclarationSyntax classDeclaration, AttributeData attribute)> classes, SourceProductionContext context)
{
    foreach (var (classDeclaration, attribute) in classes)
    {
        var properties = classDeclaration.Members.AsEnumerable().Where(o => o.IsKind(SyntaxKind.PropertyDeclaration));
        var props = properties.Select(o => o.ChildTokens().Single(n => n.IsKind(SyntaxKind.IdentifierToken)).ValueText).ToArray();
        var semanticModel = item1.GetSemanticModel(classDeclaration.GetLocation().SourceTree);
        var snapshotProperties = new List<SnapshotProperty>();
        foreach (var property in properties)
        {
            var prop = (PropertyDeclarationSyntax)property;

            snapshotProperties.Add(new SnapshotProperty(
                prop.ToString(),
                semanticModel.GetTypeInfo(prop.Type).Type.ToDisplayString(),
                prop.Identifier.ValueText,
                prop.Identifier.ValueText.ToLower()));
        }
    }
}