forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptMethodInfo.cs
More file actions
108 lines (90 loc) · 3.91 KB
/
Copy pathScriptMethodInfo.cs
File metadata and controls
108 lines (90 loc) · 3.91 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ServiceStack.Text;
namespace ServiceStack.Script
{
public class ScriptMethodInfo
{
private readonly MethodInfo methodInfo;
private readonly ParameterInfo[] @params;
public MethodInfo GetMethodInfo() => methodInfo;
public string Name => methodInfo.Name;
public string FirstParam => @params.FirstOrDefault()?.Name;
public string FirstParamType => @params.FirstOrDefault()?.ParameterType.Name;
public string ReturnType => methodInfo.ReturnType?.Name;
public int ParamCount => @params.Length;
public string[] RemainingParams => @params.Length > 1
? @params.Skip(1).Select(x => x.Name).ToArray()
: TypeConstants.EmptyStringArray;
public string[] ParamNames => @params.Select(x => x.Name).ToArray();
public string[] ParamTypes => @params.Select(x => x.ParameterType.Name.ToString()).ToArray();
public ScriptMethodInfo(MethodInfo methodInfo, ParameterInfo[] @params)
{
this.methodInfo = methodInfo ?? throw new ArgumentNullException(nameof(methodInfo));
this.@params = @params ?? throw new ArgumentNullException(nameof(@params));
}
public static List<ScriptMethodInfo> GetScriptMethods(Type scriptMethodsType, Func<MethodInfo,bool> where=null)
{
var filters = scriptMethodsType.GetMethods(BindingFlags.Instance | BindingFlags.Public);
var to = filters
.OrderBy(x => x.Name)
.ThenBy(x => x.GetParameters().Length)
.Where(x => x.DeclaringType != typeof(ScriptMethods) && x.DeclaringType != typeof(object))
.Where(m => !m.IsSpecialName);
if (where != null)
to = to.Where(where);
return to.Select(Create).ToList();
}
public static ScriptMethodInfo Create(MethodInfo mi)
{
var pis = mi.GetParameters()
.Where(x => x.ParameterType != typeof(ScriptScopeContext)).ToArray();
return new ScriptMethodInfo(mi, pis);
}
public string Return => ReturnType != null && ReturnType != nameof(StopExecution) ? " -> " + ReturnType : "";
public string Body => ParamCount == 0
? $"{Name}"
: ParamCount == 1
? $"|> {Name}"
: $"|> {Name}(" + string.Join(", ", RemainingParams) + $")";
public string ScriptSignature => ParamCount == 0
? $"{Name}{Return}"
: ParamCount == 1
? $"{FirstParam} |> {Name}{Return}"
: $"{FirstParam} |> {Name}(" + string.Join(", ", RemainingParams) + $"){Return}";
private string signature;
public string Signature
{
get
{
if (signature != null)
return signature;
var sb = StringBuilderCache.Allocate()
.Append(Name);
if (@params.Length > 0)
{
sb.Append(" (");
for (var i = 0; i < @params.Length; i++)
{
sb.Append(i > 0 ? ", " : "")
.Append(@params[i].ParameterType.Name)
.Append(" ")
.Append(@params[i].Name);
}
sb.Append(")");
}
sb.Append(Return);
return signature = StringBuilderCache.ReturnAndFree(sb);
}
}
public override string ToString() => Signature;
public ScriptMethodType ToScriptMethodType() => new ScriptMethodType {
Name = Name,
ParamNames = ParamNames,
ParamTypes = ParamTypes,
ReturnType = ReturnType,
};
}
}