forked from neuecc/MarkdownGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Beautifier.cs
36 lines (31 loc) · 1.34 KB
/
Beautifier.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace MarkdownGeneratorCore
{
public static class Beautifier
{
public static string BeautifyType(Type t, bool isFull = false)
{
if (t == null) return "";
if (t == typeof(void)) return "void";
if (!t.IsGenericType) return (isFull) ? t.FullName : t.Name;
var innerFormat = string.Join(", ", t.GetGenericArguments().Select(x => BeautifyType(x)));
return Regex.Replace(isFull ? t.GetGenericTypeDefinition().FullName : t.GetGenericTypeDefinition().Name, @"`.+$", "") + "<" + innerFormat + ">";
}
public static string ToMarkdownMethodInfo(MethodInfo methodInfo)
{
var isExtension = methodInfo.GetCustomAttributes<System.Runtime.CompilerServices.ExtensionAttribute>(false).Any();
var seq = methodInfo.GetParameters().Select(x =>
{
var suffix = x.HasDefaultValue ? (" = " + (x.DefaultValue ?? $"null")) : "";
return "`" + BeautifyType(x.ParameterType) + "` " + x.Name + suffix;
});
return methodInfo.Name + "(" + (isExtension ? "this " : "") + string.Join(", ", seq) + ")";
}
}
}