1 |
#Region "Microsoft.VisualBasic::f5419d3f4252bbbeed3dd101c5d483a3, Microsoft.VisualBasic.Core\CommandLine\Reflection\EntryPoints\Delegate.vb"
|
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 |
#End Region
|
45 |
|
46 |
Imports System.Reflection
|
47 |
Imports System.Text
|
48 |
Imports Microsoft.VisualBasic.CommandLine.ManView
|
49 |
Imports Microsoft.VisualBasic.ComponentModel
|
50 |
Imports Microsoft.VisualBasic.Text
|
51 |
|
52 |
Namespace CommandLine.Reflection.EntryPoints
|
53 |
|
54 |
Public Class APIDelegate : Implements IExportAPI
|
55 |
|
56 |
Protected _NumberOfParameters As Integer
|
57 |
Protected _metaData As Binding(Of ExportAPIAttribute, MethodInfo)
|
58 |
Protected __funcInvoker As Func(Of Object(), Integer)
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
Public ReadOnly Property Name() As String Implements IExportAPI.Name
|
67 |
Get
|
68 |
Return _metaData.Bind.Name
|
69 |
End Get
|
70 |
End Property
|
71 |
|
72 |
Public ReadOnly Property Info() As String Implements IExportAPI.Info
|
73 |
Get
|
74 |
Return _metaData.Bind.Info
|
75 |
End Get
|
76 |
End Property
|
77 |
|
78 |
Public ReadOnly Property Usage() As String Implements IExportAPI.Usage
|
79 |
Get
|
80 |
Return _metaData.Bind.Usage
|
81 |
End Get
|
82 |
End Property
|
83 |
|
84 |
Public ReadOnly Property Example() As String Implements IExportAPI.Example
|
85 |
Get
|
86 |
Return _metaData.Bind.Example
|
87 |
End Get
|
88 |
End Property
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
Public Sub New(attribute As Binding(Of ExportAPIAttribute, MethodInfo), [Invoke] As Func(Of Object(), Integer))
|
97 |
_metaData = attribute
|
98 |
__funcInvoker = Invoke
|
99 |
_metaData = attribute
|
100 |
_NumberOfParameters = 32
|
101 |
End Sub
|
102 |
|
103 |
Protected Sub New()
|
104 |
End Sub
|
105 |
|
106 |
Public Overridable Function HelpInformation(Optional md As Boolean = False) As String
|
107 |
Dim sb As New StringBuilder(1024)
|
108 |
|
109 |
If md Then
|
110 |
sb.AppendLine($"{Name}</h3>")
|
111 |
Else
|
112 |
sb.AppendLine($"Help for command ""{Name}"":")
|
113 |
End If
|
114 |
|
115 |
Call sb.AppendLine()
|
116 |
|
117 |
If md Then
|
118 |
Dim prototype$ = APIPrototype(_metaData.Target.GetFullName)
|
119 |
|
120 |
Call sb.AppendLine(Info)
|
121 |
Call sb.AppendLine()
|
122 |
Call sb.AppendLine($"**Prototype**: ``{prototype}``")
|
123 |
Call sb.AppendLine()
|
124 |
Call sb.AppendLine("###### Usage")
|
125 |
Call sb.AppendLine()
|
126 |
Call sb.AppendLine("```bash")
|
127 |
Call sb.AppendLine($"{App.AssemblyName} {Usage}")
|
128 |
Call sb.AppendLine("```")
|
129 |
|
130 |
If Not String.IsNullOrEmpty(Example) Then
|
131 |
Call sb.AppendLine("###### Example")
|
132 |
Call sb.AppendLine("```bash")
|
133 |
Call sb.AppendLine($"{App.AssemblyName} {Example}")
|
134 |
Call sb.AppendLine("```")
|
135 |
End If
|
136 |
Else
|
137 |
Dim infoLines$() = Paragraph.SplitParagraph(Info, 90).ToArray
|
138 |
|
139 |
sb.AppendLine(String.Format(" Information: {0}", infoLines.FirstOrDefault))
|
140 |
|
141 |
If infoLines.Length > 1 Then
|
142 |
For Each line$ In infoLines.Skip(1)
|
143 |
Call sb.AppendLine($" {line}")
|
144 |
Next
|
145 |
End If
|
146 |
|
147 |
sb.AppendLine(String.Format(" Usage: {0} {1}", Application.ExecutablePath, Usage))
|
148 |
sb.AppendLine(String.Format(" Example: {0} {1}", App.AssemblyName, Example))
|
149 |
End If
|
150 |
|
151 |
Return sb.ToString
|
152 |
End Function
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
<param name="parameters">数组的长度必须与目标函数的参数的数目一致,否则短于目标函数的参数的数目的数组会使用Nothing来填充缺少的部分,而多于目标函数的参数会被截断</param>
|
158 |
|
159 |
|
160 |
Public Function Execute(parameters As Object()) As Integer
|
161 |
Dim callParameters() As Object
|
162 |
|
163 |
If parameters.Length < _NumberOfParameters Then
|
164 |
callParameters = New Object(_NumberOfParameters - 1) {}
|
165 |
Call parameters.CopyTo(callParameters, 0)
|
166 |
ElseIf parameters.Length > _NumberOfParameters Then
|
167 |
callParameters = New Object(_NumberOfParameters - 1) {}
|
168 |
Call Array.ConstrainedCopy(parameters, 0, callParameters, 0, _NumberOfParameters)
|
169 |
Else
|
170 |
callParameters = parameters
|
171 |
End If
|
172 |
|
173 |
Return __funcInvoker.Invoke(callParameters)
|
174 |
End Function
|
175 |
|
176 |
Public Overrides Function ToString() As String
|
177 |
Return Name
|
178 |
End Function
|
179 |
End Class
|
180 |
End Namespace
|