Repository navigation
AutoDiagram Integration
MermaidSharp.AutoDiagram is a .NET extension for MermaidSharp that automatically generates Mermaid diagrams from .NET types and assemblies using reflection — no manual diagram definition required.
Targets: .NET Framework 4.8 and .NET 8.0
Install the NuGet package:
dotnet add package MermaidSharp.AutoDiagram
ClassDiagramExtension provides extension methods to generate a ClassDiagram from .NET types or assemblies using reflection.
Detected relationships are:
| Relationship | Description |
|---|---|
| Association | A class holds a property of another class present in the diagram. |
| Inherited | A class inherits from another class present in the diagram. |
| Interface | A class implements an interface present in the diagram. |
Note: Only types within the provided set (types or assemblies) are linked. External framework types are ignored.
using MermaidSharp.AutoDiagram.Diagrams;
var diagram = typeof(MyClass).ToMermaidClassDiagram();
Console.WriteLine(diagram.CalculateDiagram());
var diagram = new[] { typeof(Order), typeof(Customer), typeof(Product) }
.ToMermaidClassDiagram();
Console.WriteLine(diagram.CalculateDiagram());
All public types in the assembly are included. Each assembly maps to a namespace block in the diagram.
var diagram = typeof(MyClass).Assembly.ToMermaidClassDiagram();
Console.WriteLine(diagram.CalculateDiagram());
var diagram = new[]
{
typeof(Order).Assembly,
typeof(CustomerService).Assembly
}
.ToMermaidClassDiagram();
Console.WriteLine(diagram.CalculateDiagram());
Pass a ClassDiagramOptions instance to any overload to customize the output:
using MermaidSharp.AutoDiagram;
using MermaidSharp.AutoDiagram.Diagrams;
using MermaidSharp.AutoDiagram.Enums;
using MermaidSharp.AutoDiagram.Options;
using MermaidSharp.Enums;
using System;
using System.Collections.Generic;
var options = new ClassDiagramOptions
{
// Only include public and protected classes
IncludeClassesVisibility = new List<ClassPropertyVisibility>
{
ClassPropertyVisibility.Public,
ClassPropertyVisibility.Protected
},
// Only include types from a specific namespace
AssemblyClassFilter = t => t.Namespace?.StartsWith("MyApp.Models") == true,
PropertyOptions = new ClassPropertyOptions
{
IncludeVisibility = new List<ClassPropertyVisibility>
{
ClassPropertyVisibility.Public,
ClassPropertyVisibility.Protected
}
},
MethodOptions = new ClassMethodOptions
{
IncludeVisibility = new List<ClassPropertyVisibility>
{
ClassPropertyVisibility.Public
},
IncludeParameters = true,
IncludeReturnType = true,
IncludeReturnVoid = false // void methods are excluded
},
LinkOptions = new ClassLinkOptions
{
IncludeLinks = ClassLinkOption.Association | ClassLinkOption.Inherited,
IncludeLinksLabels = true
}
};
var diagram = typeof(MyClass).Assembly.ToMermaidClassDiagram(options);
Console.WriteLine(diagram.CalculateDiagram());
| Property | Type | Default | Description |
|---|---|---|---|
IncludeClassesVisibility |
List<ClassPropertyVisibility> |
All visibilities | Visibility levels of classes to include. |
AssemblyClassFilter |
Func<Type, bool> |
Excludes auto-generated classes | Predicate to filter which types are included. |
PropertyOptions |
ClassPropertyOptions |
See below | Controls property generation. |
MethodOptions |
ClassMethodOptions |
See below | Controls method generation. |
LinkOptions |
ClassLinkOptions |
See below | Controls relationship link generation. |
| Property | Type | Default | Description |
|---|---|---|---|
IncludeVisibility |
List<ClassPropertyVisibility> |
All visibilities | Visibility levels of properties to include. |
| Property | Type | Default | Description |
|---|---|---|---|
IncludeVisibility |
List<ClassPropertyVisibility> |
All visibilities | Visibility levels of methods to include. |
IncludeParameters |
bool |
true |
Whether to include method parameters. |
IncludeReturnType |
bool |
true |
Whether to include the return type. |
IncludeReturnVoid |
bool |
false |
Whether to include void methods. |
| Property | Type | Default | Description |
|---|---|---|---|
IncludeLinks |
ClassLinkOption |
All |
Combination of relationship types to include (flags enum). |
IncludeLinksLabels |
bool |
true |
Whether to display labels on relationship links. |
| Value | Description |
|---|---|
None |
No relationships are included. |
Association |
Includes property-based associations between classes. |
Interface |
Includes interface implementation relationships. |
Inherited |
Includes class inheritance relationships. |
All |
Includes all relationship types (`Association |
FlowchartDiagramExtension generates a FlowchartDiagram that shows the dependency relationships between assemblies. Each assembly becomes a node and its referenced assemblies become linked nodes.
using MermaidSharp.AutoDiagram.Diagrams;
using System;
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
var diagram = assemblies.ToMermaidFlowchartDiagram();
Console.WriteLine(diagram.CalculateDiagram());
By default, all nodes are wrapped inside a subgraph labelled "Project".
using MermaidSharp.AutoDiagram.Diagrams;
using MermaidSharp.AutoDiagram.Options;
var options = new FlowchartDiagramOptions
{
// Set to null or empty string to disable subgraph grouping
SubgraphGroupLabel = "MyApplication"
};
var diagram = assemblies.ToMermaidFlowchartDiagram(options);
Console.WriteLine(diagram.CalculateDiagram());
| Property | Type | Default | Description |
|---|---|---|---|
SubgraphGroupLabel |
string |
"Project" |
Label of the subgraph wrapping all nodes. Set to null or empty to place nodes directly in the root graph. |