Skip to content

Commit 4c0d2ed

Browse files
committed
Improved name of generic classes (coverlet-coverage/coverlet#1077)
1 parent 46ae4cc commit 4c0d2ed

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/Readme.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ For further details take a look at LICENSE.txt.
6363

6464
CHANGELOG
6565

66+
4.8.6.0
67+
68+
* New: Improved name of generic classes (https://github.com/coverlet-coverage/coverlet/issues/1077)
69+
6670
4.8.5.0
6771

6872
* Fix: #406: Fixed reference version of McMaster.NETCore.Plugins for package ReportGenerator.Core

src/ReportGenerator.Core/Parser/Analysis/Class.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Text.RegularExpressions;
45

56
namespace Palmmedia.ReportGenerator.Core.Parser.Analysis
67
{
@@ -9,6 +10,11 @@ namespace Palmmedia.ReportGenerator.Core.Parser.Analysis
910
/// </summary>
1011
public class Class
1112
{
13+
/// <summary>
14+
/// Regex to analyze if a class is generic.
15+
/// </summary>
16+
private static Regex genericClassRegex = new Regex("^(?<Name>.+)`(?<Number>\\d+)$", RegexOptions.Compiled);
17+
1218
/// <summary>
1319
/// The object to lock the class add.
1420
/// </summary>
@@ -38,6 +44,47 @@ internal Class(string name, Assembly assembly)
3844
{
3945
this.Name = name ?? throw new ArgumentNullException(nameof(name));
4046
this.Assembly = assembly ?? throw new ArgumentNullException(nameof(assembly));
47+
48+
/*
49+
* Convert class name of generic classes:
50+
* See: https://github.com/coverlet-coverage/coverlet/issues/1077
51+
*
52+
* SomeClass`1 -> SomeClass<T>
53+
* SomeClass`2 -> SomeClass<T1, T2>
54+
* SomeClass`3 -> SomeClass<T1, T2, T3>
55+
*/
56+
if (name.Contains("`"))
57+
{
58+
Match match = genericClassRegex.Match(name);
59+
60+
if (match.Success)
61+
{
62+
this.Name = match.Groups["Name"].Value;
63+
64+
int number = int.Parse(match.Groups["Number"].Value);
65+
66+
if (number == 1)
67+
{
68+
this.Name += "<T>";
69+
}
70+
else if (number > 1)
71+
{
72+
this.Name += "<";
73+
74+
for (int i = 1; i <= number; i++)
75+
{
76+
if (i > 1)
77+
{
78+
this.Name += ", ";
79+
}
80+
81+
this.Name += "T" + i;
82+
}
83+
84+
this.Name += ">";
85+
}
86+
}
87+
}
4188
}
4289

4390
/// <summary>

0 commit comments

Comments
 (0)