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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
@page "/CSTJApiComparison"
@using System.Reflection
@using System.Text
@using System.Text.Json
@using LibMatrix.Extensions
<PageTitle>Counter</PageTitle>
<h3>Compare STJ API</h3>
// side by side table
<table class="table table-bordered">
<thead>
<tr>
@foreach (var type in TypesToCompare) {
<th>@type.Name</th>
}
</tr>
</thead>
<tbody>
@* // display all public static methods in matching rows, put non matching at the end, by signature *@
@* // do not use type.GetMethod as it throws ambiguous match errors *@
@{ var methods = TypesToCompare.Select(t => (Type: t, Methods: t.GetMethods())); }
@foreach (var method in methods.SelectMany(m => m.Methods).DistinctBy(m => GetMethodSignature(m))) {
<tr>
@foreach (var (type, _) in methods) {
var methodInfo = type.GetMethods().FirstOrDefault(m => GetMethodSignature(m) == GetMethodSignature(method));
if (methodInfo != null) {
<td>@GetMethodSignature(methodInfo)</td>
}
else {
<td></td>
}
}
</tr>
}
</tbody>
</table>
@code {
private static readonly Type[] TypesToCompare = [typeof(JsonSerializer), typeof(CanonicalJsonSerializer)];
private string GetMethodSignature(MethodInfo method, bool includeModifiers = true, bool includeReturnType = true, bool includeParameters = true) {
var sb = new StringBuilder();
//modifiers
if (includeModifiers) {
if (method.IsPublic) {
sb.Append("public ");
}
if (method.IsStatic) {
sb.Append("static ");
}
}
//return type
if (includeReturnType) {
if (method.ReturnType.IsGenericType) {
sb.Append(method.ReturnType.Name.Split('`')[0]);
sb.Append("<");
var genericArguments = method.ReturnType.GetGenericArguments();
foreach (var genericArgument in genericArguments) {
sb.Append(genericArgument.Name);
if (genericArgument != genericArguments.Last())
sb.Append(", ");
}
sb.Append(">");
}
else {
//lowercase primitives
sb.Append(method.ReturnType.Name);
}
sb.Append(' ');
}
sb.Append(method.Name);
if (method.IsGenericMethod) {
sb.Append("<");
var genericArguments = method.GetGenericArguments();
foreach (var genericArgument in genericArguments) {
sb.Append(genericArgument.Name);
if (genericArgument != genericArguments.Last())
sb.Append(", ");
}
sb.Append(">");
}
sb.Append("(");
var parameters = method.GetParameters();
foreach (var parameter in parameters) {
//handle generics
if (parameter.ParameterType.IsGenericType) {
sb.Append(parameter.ParameterType.Name.Split('`')[0]);
sb.Append("<");
var genericArguments = parameter.ParameterType.GetGenericArguments();
foreach (var genericArgument in genericArguments) {
sb.Append(genericArgument.Name);
if (genericArgument != genericArguments.Last())
sb.Append(", ");
}
sb.Append(">");
}
else {
sb.Append(parameter.ParameterType.Name);
}
sb.Append(" ");
sb.Append(parameter.Name);
if (parameter.HasDefaultValue) {
sb.Append(" = ");
//handle default value
if (parameter.DefaultValue == null) {
sb.Append("null");
}
else if (parameter.ParameterType == typeof(string)) {
sb.Append($"\"{parameter.DefaultValue}\"");
}
else {
sb.Append(parameter.DefaultValue);
}
}
if (parameter != parameters.Last())
sb.Append(", ");
}
sb.Append(")");
return sb.ToString();
}
}
|