Skip to content

自动类图生成

Github: orrest/PlantUmlClassDiagramGenerator Fork 自 pierre3/PlantUmlClassDiagramGenerator 项目。

Project Overview

PlantUmlClassDiagramGenerator is a tool that automatically generates PlantUML class diagrams from C# source code. The project provides multiple integration options to help developers quickly visualize C# code structure and relationships.


Core Features

The tool analyzes C# source code and generates standard PlantUML class diagrams, supporting:

  • Type declarations: including class, struct, interface, enum, record types
  • Type modifiers: abstract, static, partial, sealed modifiers
  • Member visibility: public, private, protected, internal access modifiers
  • Relationship mapping: automatic identification of inheritance, implementation, and association relationships

Roslyn Source Generator

The most advanced approach: automatically generating class diagrams at compile time using a Source Generator.
This method uses compiler symbol information rather than syntax trees for more accurate code structure analysis.

Steps:

  1. Install the NuGet package in the project you want to generate class diagrams from:

    PlantUML.ClsDig.Src.Gen
    

    (If your class files are distributed across multiple projects, install the package in each one.)

  2. In your project's .csproj file:

    <PropertyGroup>
        <DefineConstants>$(DefineConstants);GENERATE_PLANTUML</DefineConstants>
    </PropertyGroup>
    <ItemGroup>
        <CompilerVisibleProperty Include="PlantUmlGenerator_OutputDir" />
    </ItemGroup>
    <PropertyGroup>
        <PlantUmlGenerator_OutputDir>$(SolutionDir)generated-uml</PlantUmlGenerator_OutputDir>
    </PropertyGroup>
    
    • The constant GENERATE_PLANTUML triggers the generator.
    • If you have class diagrams across multiple projects, specify the same output folder so they can reference each other.
  3. Add the attribute in your AssemblyInformation.cs or directly on classes:

    [assembly: PlantUmlClassDiagramGenerator.SourceGenerator.Attributes.PlantUmlDiagram]
    
    4. Build the project, and you'll see the .puml files in the PlantUmlGenerator_OutputDir folder you specified.

See the original README.


What Is This Fork For?

  1. Added reddress-darkblue as the default theme in the source generator library.
  2. Specified the direction of the associations in the source generator:
    • Implementations and inheritances are vertical
    • Compositions, aggregations, and dependencies are horizontal

Note

  • If the theme is not previewing correctly in VS Code, consider updating the PlantUML version.
  • For the usage of VS extension and the dotnet CLI tool, see the original project.