1 | #Region "Microsoft.VisualBasic::0e079b2e518e7715ae5eabe2b0754b2f, Microsoft.VisualBasic.Core\Scripting\MetaData\Parameter.vb" |
2 | |
3 | ' Author: |
4 | ' |
5 | ' asuka (amethyst.asuka@gcmodeller.org) |
6 | ' xie (genetics@smrucc.org) |
7 | ' xieguigang (xie.guigang@live.com) |
8 | ' |
9 | ' Copyright (c) 2018 GPL3 Licensed |
10 | ' |
11 | ' |
12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
13 | ' |
14 | ' |
15 | ' This program is free software: you can redistribute it and/or modify |
16 | ' it under the terms of the GNU General Public License as published by |
17 | ' the Free Software Foundation, either version 3 of the License, or |
18 | ' (at your option) any later version. |
19 | ' |
20 | ' This program is distributed in the hope that it will be useful, |
21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | ' GNU General Public License for more details. |
24 | ' |
25 | ' You should have received a copy of the GNU General Public License |
26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
27 | |
28 | |
29 | |
30 | ' /********************************************************************************/ |
31 | |
32 | ' Summaries: |
33 | |
34 | ' Class Parameter |
35 | ' |
36 | ' Properties: [Alias], Description, ParameterInfo, TypeInfo |
37 | ' |
38 | ' Constructor: (+1 Overloads) Sub New |
39 | ' Function: GetAliasNameView, (+2 Overloads) GetParameterNameAlias, ToString |
40 | ' |
41 | ' |
42 | ' /********************************************************************************/ |
43 | |
44 | #End Region |
45 | |
46 | Imports System.Reflection |
47 | Imports Microsoft.VisualBasic.ComponentModel.Collection.Generic |
48 | |
49 | Namespace Scripting.MetaData |
50 | |
51 | ''' <summary> |
52 | ''' You Cann assign the parameter value using the parameter's alias name in the scripting using this attribute. |
53 | ''' (你可以使用本属性将函数的参数名进行重命名,这样子你就可以使用本属性得到一个书写更加漂亮的编程脚本文件了) |
54 | ''' </summary> |
55 | ''' <remarks></remarks> |
56 | <AttributeUsage(AttributeTargets.Parameter, AllowMultiple:=False, Inherited:=True)> |
57 | Public Class Parameter : Inherits Attribute |
58 | Implements INamedValue |
59 | |
60 | ''' <summary> |
61 | ''' 请使用这个方法<see cref="Parameter.GetParameterNameAlias"></see>来获取参数信息 |
62 | ''' </summary> |
63 | ''' <value></value> |
64 | ''' <returns></returns> |
65 | ''' <remarks></remarks> |
66 | Public ReadOnly Property ParameterInfo As ParameterInfo |
67 | |
68 | Dim _alias As String |
69 | |
70 | ''' <summary> |
71 | ''' The alias name of this function parameter in the scripting.(脚本函数的参数的别名) |
72 | ''' </summary> |
73 | ''' <value></value> |
74 | ''' <returns></returns> |
75 | ''' <remarks></remarks> |
76 | Public Property [Alias] As String Implements INamedValue.Key |
77 | Get |
78 | If String.IsNullOrEmpty(_alias) AndAlso Not ParameterInfo Is Nothing Then |
79 | _alias = ParameterInfo.Name |
80 | End If |
81 | |
82 | Return _alias |
83 | End Get |
84 | Protected Set(value As String) |
85 | _alias = value |
86 | End Set |
87 | End Property |
88 | |
89 | ''' <summary> |
90 | ''' The description information in the scripting help system.(在帮助信息里面进行显示的本参数的简要的描述信息) |
91 | ''' </summary> |
92 | ''' <value></value> |
93 | ''' <returns></returns> |
94 | ''' <remarks></remarks> |
95 | Public ReadOnly Property Description As String |
96 | |
97 | ''' <summary> |
98 | ''' You can using this attribute to customize your API interface. |
99 | ''' </summary> |
100 | ''' <param name="Alias">The alias name of this function parameter in the scripting.(当前脚本函数的这个参数的别名)</param> |
101 | ''' <param name="MyDescription">The description information in the scripting help system.(这个信息会显示在脚本环境的帮助系统之中)</param> |
102 | ''' <remarks></remarks> |
103 | Sub New([Alias] As String, Optional MyDescription As String = "") |
104 | _alias = [Alias] |
105 | _Description = MyDescription |
106 | End Sub |
107 | |
108 | Public Overrides Function ToString() As String |
109 | Return _alias |
110 | End Function |
111 | |
112 | ''' <summary> |
113 | ''' 当没有定义属性的时候,会返回参数名 |
114 | ''' </summary> |
115 | ''' <param name="pInfo"></param> |
116 | ''' <returns></returns> |
117 | Public Shared Function GetAliasNameView(pInfo As ParameterInfo) As String |
118 | Dim [alias] = GetParameterNameAlias(pInfo, False) |
119 | |
120 | If [alias] Is Nothing Then |
121 | Return pInfo.Name |
122 | Else |
123 | If String.IsNullOrEmpty([alias].Alias) Then |
124 | Return pInfo.Name |
125 | Else |
126 | Return [alias].Alias |
127 | End If |
128 | End If |
129 | End Function |
130 | |
131 | Public Shared Function GetParameterNameAlias(pInfo As ParameterInfo, AutoFill As Boolean) As Parameter |
132 | Dim attrs As Object() = pInfo.GetCustomAttributes(GetType(Parameter), inherit:=True) |
133 | If attrs.IsNullOrEmpty Then |
134 | |
135 | If Not AutoFill Then |
136 | Return Nothing |
137 | End If |
138 | |
139 | Return New Parameter(pInfo.Name) With {._ParameterInfo = pInfo} |
140 | Else |
141 | Dim value = DirectCast(attrs.First, Parameter) |
142 | value._ParameterInfo = pInfo |
143 | Return value |
144 | End If |
145 | End Function |
146 | |
147 | Public Shared Function GetParameterNameAlias(pInfo As ParameterInfo, [Default] As Parameter) As Parameter |
148 | Dim value = GetParameterNameAlias(pInfo, False) |
149 | If value Is Nothing Then |
150 | If [Default] Is Nothing Then [Default] = New Parameter(pInfo.Name) |
151 | [Default]._ParameterInfo = pInfo |
152 | Return [Default] |
153 | Else |
154 | Return value |
155 | End If |
156 | End Function |
157 | |
158 | Public Shared ReadOnly Property TypeInfo As Type = GetType(Parameter) |
159 | End Class |
160 | End Namespace |