Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

源代码生成类型的方法导出功能 #39

Merged
merged 51 commits into from
Sep 2, 2023

Conversation

lindexi
Copy link
Member

@lindexi lindexi commented Jun 30, 2023

在分部类里定义分部方法,分部方法标记 dotnetCampus.Telescope.TelescopeExportAttribute 特性,且返回值包括导出条件,如以下写法

internal partial class Program
{
    [dotnetCampus.Telescope.TelescopeExportAttribute()]
    private static partial IEnumerable<(Type type, F1Attribute attribute, Func<Base> creator)> ExportFooEnumerable();
}

以上代码将导出当前项目标记了 F1Attribute 且继承 Base 的所有类型。经过 Telescope 源代码生成器即可生成大概如下的代码

    [global::System.CodeDom.Compiler.GeneratedCode("dotnetCampus.Telescope.SourceGeneratorAnalyzers", "1.0.0")]
    internal partial class Program
    {
        private static partial IEnumerable<(Type type, F1Attribute attribute, Func<Base> creator)> ExportFooEnumerable()
        {
            yield return (typeof(F2), new F1Attribute()
            {
                       
            }, () => new F2());
            yield return (typeof(F3), new F1Attribute()
            {
                       
            }, () => new F3());
        }
    }

高级用法:

可以在 TelescopeExportAttribute 加上 IncludeReference 属性用来导出所有引用程序集的满足条件的类型,如以下代码

internal partial class Program
{
    [dotnetCampus.Telescope.TelescopeExportAttribute(IncludeReference = true)]
    private static partial IEnumerable<(Type type, F1Attribute attribute, Func<Base> creator)> ExportFooEnumerable();
}

Close #35

@lindexi lindexi changed the title WIP 方法导出 WIP 源代码生成类型的方法导出功能 Aug 29, 2023
@lindexi lindexi changed the title WIP 源代码生成类型的方法导出功能 源代码生成类型的方法导出功能 Aug 30, 2023
@lindexi lindexi requested a review from walterlv August 30, 2023 07:24

public static class ValueTupleInfoParser
{
public static bool TryParse(ITypeSymbol type, CancellationToken token, out ValueTupleInfo valueTupleInfo)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[NotNullWhen(true)]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@walterlv 似乎除了安装你的库之外,我没有找到其他好方法加上这个特性

Comment on lines +24 to +36
$@"//------------------------------------------------------------------------------
// <auto-generated>
// 此代码由工具生成。
//
// 对此文件的更改可能会导致不正确的行为,并且如果
// 重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------

#define GENERATED_CODE

";
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

从今天开始,你可以使用 """ 来编写这种代码,对得齐多好

/// <param name="typeSymbol"></param>
/// <param name="classDeclarationList">嵌套类的定义</param>
/// <returns></returns>
private static bool TryGetClassDeclarationList(INamedTypeSymbol typeSymbol, out List<string>? classDeclarationList)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[NotNullWhen(true)]

// ReSharper disable InconsistentNaming
public static DiagnosticDescriptor Tes000_UnknownError => new DiagnosticDescriptor
(
nameof(Tes000),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我看大家编译诊断号的命名都是全大写缩写词,但你用帕斯卡缩写?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

举例 CS8002 CA1803 IDE1036

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@walterlv 似乎这部分没有什么规范,看起来改起来也比较麻烦,就先保持这样吧

[assembly: dotnetCampus.Telescope.MarkExportAttribute(typeof(Base), typeof(FooAttribute))]
```

标记之后将会自动生成 `dotnetCampus.Telescope.__AttributedTypesExport__` 类型,即可在代码里面直接使用,如以下代码
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

之前我们使用 __ 下划线名字是因为代码不应该被用户接触。但你现在被用户接触了,需要改名吧……

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@walterlv 改名的话,不能无缝对接原来的。我想做的是一个平替

新的话感觉使用分部方法更加合适,这样可以减少多一个程序集的引用且不用反射,也没有开发过程中缺乏语义提示问题

@lindexi lindexi merged commit cd16583 into master Sep 2, 2023
1 check passed
@lindexi lindexi deleted the t/lindexi/TelescopeExport branch September 2, 2023 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

使用分部方法优化代码编写
2 participants