-
Notifications
You must be signed in to change notification settings - Fork 0
/
MethodInfoExtension.cs
128 lines (108 loc) · 3.45 KB
/
MethodInfoExtension.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
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
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
namespace Aya.Extension
{
public static class MethodInfoExtension
{
#region Run
public static void Execute(this MethodInfo methodInfo, object target, params object[] param)
{
if (methodInfo.IsStatic)
{
var parameters = new object[param.Length + 1];
parameters[0] = target;
for (var i = 0; i < param.Length; ++i)
{
parameters[i + 1] = param[i];
}
methodInfo.Invoke(null, parameters);
}
else
{
methodInfo.Invoke(target, param);
}
}
#endregion
#region Check
public static bool IsExtensionMethod(this MethodBase methodInfo)
{
var declaringType = methodInfo.DeclaringType;
if (declaringType == null) return false;
if (declaringType.IsSealed && !declaringType.IsGenericType && !declaringType.IsNested)
{
return methodInfo.IsDefined(typeof(ExtensionAttribute), false);
}
return false;
}
#endregion
#region Signature
public static string GetSignature(this MethodInfo methodInfo)
{
if (methodInfo == null)
{
return "";
}
var stringBuilder = new StringBuilder();
if (methodInfo.IsPrivate)
{
stringBuilder.Append("private ");
}
else if (methodInfo.IsPublic)
{
stringBuilder.Append("public ");
}
if (methodInfo.IsPrivate)
{
stringBuilder.Append("private ");
}
else if (methodInfo.IsPublic)
{
stringBuilder.Append("public ");
}
else if (methodInfo.IsFamily)
{
stringBuilder.Append("protected ");
}
else if (methodInfo.IsAssembly)
{
stringBuilder.Append("internal ");
}
else if (methodInfo.IsFamilyOrAssembly)
{
stringBuilder.Append("protected internal ");
}
if (methodInfo.IsStatic)
{
stringBuilder.Append("static ");
}
if (methodInfo.IsAbstract)
{
stringBuilder.Append("abstract ");
}
else if (methodInfo.IsVirtual)
{
stringBuilder.Append("virtual ");
}
stringBuilder.Append(methodInfo.ReturnType.Name);
stringBuilder.Append(" ");
stringBuilder.Append(methodInfo.Name);
stringBuilder.Append("(");
var parameters = methodInfo.GetParameters();
for (var i = 0; i < parameters.Length; i++)
{
var parameter = parameters[i];
stringBuilder.Append(parameter.ParameterType.Name);
stringBuilder.Append(" ");
stringBuilder.Append(parameter.Name);
if (i < parameters.Length - 1)
{
stringBuilder.Append(",");
}
}
stringBuilder.Append(")");
return stringBuilder.ToString();
}
#endregion
}
}