-
Notifications
You must be signed in to change notification settings - Fork 1
/
Factory.TypeNames.cs
48 lines (41 loc) · 1.94 KB
/
Factory.TypeNames.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System.Diagnostics;
using ICSharpCode.Decompiler.TypeSystem;
namespace NetAmermaid
{
public partial class ClassDiagrammerFactory
{
/// <summary>Returns a cached display name for <paramref name="type"/>.</summary>
private string GetName(IType type)
{
if (labels!.ContainsKey(type)) return labels[type]; // return cached value
return labels[type] = GenerateName(type); // generate and cache new value
}
/// <summary>Generates a display name for <paramref name="type"/>.</summary>
private string GenerateName(IType type)
{
// non-generic types
if (type.TypeParameterCount < 1)
{
if (type is ArrayType array) return GetName(array.ElementType) + "[]";
if (type is ByReferenceType byReference) return "&" + GetName(byReference.ElementType);
ITypeDefinition? typeDefinition = type.GetDefinition();
if (typeDefinition == null)
{
if (type.Kind != TypeKind.TypeParameter && type.Kind != TypeKind.Dynamic) Debugger.Break();
return type.Name;
}
if (typeDefinition.KnownTypeCode == KnownTypeCode.None)
{
if (type.DeclaringType == null) return type.Name.Replace('<', '❰').Replace('>', '❱'); // for module of executable
else return type.DeclaringType.Name + '+' + type.Name; // nested types
}
return KnownTypeReference.GetCSharpNameByTypeCode(typeDefinition.KnownTypeCode) ?? type.Name;
}
// nullable types
if (type.TryGetNullableType(out var nullableType)) return GetName(nullableType) + "?";
// other generic types
string typeArguments = type.TypeArguments.Select(GetName).Join(", ");
return type.Name + $"❰{typeArguments}❱";
}
}
}