Skip to content

附录 A:文件路径导航

本页中的所有路径都按“仓库根目录相对路径”书写,不是相对于当前网页文件的路径。

这份附录帮你在主案例里快速定位“生成器写在哪、示例怎么用、测试怎么验证、生成结果去哪里看”。

生成器项目

仓库路径用途
sample/01-tostring-generator/ToStringGenerator/ToStringGenerator.cs主生成器实现,包含 Initialize(...)TransformClass(...)GenerateToStringCode(...)
sample/01-tostring-generator/ToStringGenerator/Models/ClassToGenerate.cs类级中间模型,保存类名、命名空间、泛型、嵌套结构等信息
sample/01-tostring-generator/ToStringGenerator/Models/PropertyInfo.cs属性级中间模型,保存属性名、类型、可空信息等
sample/01-tostring-generator/ToStringGenerator/ToStringGenerator.csproj生成器工程配置,决定它如何被示例工程以 Analyzer 引用方式接入编译

示例项目

仓库路径用途
sample/01-tostring-generator/ToStringGenerator.Sample/ToStringGenerator.Sample.csproj示例工程配置,展示如何引用生成器
sample/01-tostring-generator/ToStringGenerator.Sample/Program.cs程序入口,用来运行并观察生成的 ToString() 结果
sample/01-tostring-generator/ToStringGenerator.Sample/Examples/SimpleClass.cs最容易理解的基础示例
sample/01-tostring-generator/ToStringGenerator.Sample/Examples/ComplexClass.cs展示多个属性和更接近真实业务的类
sample/01-tostring-generator/ToStringGenerator.Sample/Examples/GenericClass.cs用来验证泛型类生成
sample/01-tostring-generator/ToStringGenerator.Sample/Examples/NestedClass.cs用来验证嵌套类生成
sample/01-tostring-generator/ToStringGenerator.Sample/Examples/EmptyClass.cs用来观察边界情况和空属性列表处理

测试项目

仓库路径用途
sample/01-tostring-generator/ToStringGenerator.Tests/TestHelpers.cs构造编译环境、运行生成器的测试辅助代码
sample/01-tostring-generator/ToStringGenerator.Tests/ToStringGeneratorTests.cs针对生成结果做断言的单元测试
sample/01-tostring-generator/ToStringGenerator.Tests/ToStringGenerator.Tests.csproj测试工程配置

生成结果位置

仓库路径你通常会在这里看什么
sample/01-tostring-generator/ToStringGenerator.Sample/obj/Debug/net8.0/generated实际生成的 .g.cs 文件
sample/01-tostring-generator/ToStringGenerator.Sample/obj/Debug/net8.0构建中间产物,排查生成器是否参与编译时也常会看这里

最推荐的阅读顺序

  1. 先看 ToStringGenerator.cs
  2. 再看 ClassToGenerate.csPropertyInfo.cs
  3. 再看 Program.csExamples/*.cs
  4. 最后看 ToStringGeneratorTests.cs

如果你希望先看一页“带源码片段的地图版说明”,先读 源码导读

基于当前仓库文档副本构建的 VitePress 站点