1 #Region "Microsoft.VisualBasic::d769d01cfe5863149e541557a9abb7e8, Microsoft.VisualBasic.Core\ApplicationServices\VBDev\ApplicationInfoUtils.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     '     Module ApplicationInfoUtils
35     
36     '         Function: CurrentExe, FromAssembly, FromTypeModule, GetCompanyName, GetCopyRightsDetail
37     '                   GetGuid, GetProductDescription, GetProductName, GetProductTitle, GetProductVersion
38     '                   VBCore
39     
40     
41     ' /********************************************************************************/
42
43 #End Region
44
45 Imports System.Reflection
46 Imports System.Runtime.CompilerServices
47 Imports System.Runtime.InteropServices
48
49 Namespace ApplicationServices.Development
50
51     ''' <summary>
52     ''' Parsing product assembly meta data
53     ''' </summary>
54     ''' <remarks>
55     ''' http://www.c-sharpcorner.com/uploadfile/ravesoft/access-assemblyinfo-file-and-get-product-informations/
56     ''' </remarks>
57     Public Module ApplicationInfoUtils
58
59         <Extension>
60         Public Function FromAssembly(assm As Assembly) As AssemblyInfo
61             Return New AssemblyInfo With {
62                 .AssemblyCompany = GetCompanyName(assm),
63                 .AssemblyFileVersion = GetProductVersion(assm),
64                 .AssemblyProduct = GetProductName(assm),
65                 .AssemblyCopyright = GetCopyRightsDetail(assm),
66                 .AssemblyTitle = GetProductTitle(assm),
67                 .AssemblyDescription = GetProductDescription(assm),
68                 .Guid = GetGuid(assm)
69             }
70         End Function
71
72         ''' <summary>
73         ''' 获取对当前运行时环境的描述
74         ''' </summary>
75         ''' <returns></returns>
76         <MethodImpl(MethodImplOptions.AggressiveInlining)>
77         Public Function VBCore() As AssemblyInfo
78             Return GetType(ApplicationInfoUtils).Assembly.FromAssembly
79         End Function
80
81         ''' <summary>
82         ''' 获取当前进程的exe文件的程序描述信息,直接使用New申明得到的只是运行时核心模块dll文件的信息
83         ''' </summary>
84         ''' <returns></returns>
85         Public Function CurrentExe() As AssemblyInfo
86             Try
87                 Return Assembly.LoadFile(App.ExecutablePath).FromAssembly
88             Catch ex As Exception
89                 ex = New Exception(App.ExecutablePath, ex)
90                 Call App.LogException(ex)
91                 Return Nothing
92             End Try
93         End Function
94
95         <MethodImpl(MethodImplOptions.AggressiveInlining)>
96         <Extension>
97         Public Function FromTypeModule(type As Type) As AssemblyInfo
98             Return type.Assembly.FromAssembly
99         End Function
100
101         Public Function GetGuid(assm As Assembly) As String
102             If assm IsNot Nothing Then
103                 Dim attrs = assm.GetCustomAttributes(GetType(GuidAttribute), False)
104
105                 If attrs.IsNullOrEmpty Then
106                     Return ""
107                 Else
108                     Return DirectCast(attrs(Scan0), GuidAttribute).Value
109                 End If
110             Else
111                 Return ""
112             End If
113         End Function
114
115         ''' <summary>
116         ''' Get the name of the system provider name from the assembly
117         ''' </summary>
118         ''' <param name="assm"></param>
119         ''' <returns></returns>
120         Public Function GetCompanyName(assm As Assembly) As String
121             If assm IsNot Nothing Then
122                 Dim companyName As String = ""
123                 Dim customAttributes As Object() = assm.GetCustomAttributes(GetType(AssemblyCompanyAttribute), False)
124                 If (customAttributes IsNot NothingAndAlso (customAttributes.Length > 0) Then
125                     companyName = DirectCast(customAttributes(0), AssemblyCompanyAttribute).Company
126                 End If
127                 If String.IsNullOrEmpty(companyName) Then
128                     companyName = String.Empty
129                 End If
130
131                 Return companyName
132             Else
133                 Return ""
134             End If
135         End Function
136
137         ''' <summary>
138         ''' Get System version from the assembly
139         ''' </summary>
140         ''' <param name="assembly"></param>
141         ''' <returns></returns>
142         Public Function GetProductVersion(assembly As Assembly) As String
143             If assembly IsNot Nothing Then
144                 Dim productVersion As String = ""
145                 Dim customAttributes As Object() = assembly.GetCustomAttributes(GetType(AssemblyVersionAttribute), False)
146                 If (customAttributes IsNot NothingAndAlso (customAttributes.Length > 0) Then
147                     productVersion = DirectCast(customAttributes(0), AssemblyVersionAttribute).Version
148                 End If
149                 If String.IsNullOrEmpty(productVersion) Then
150                     productVersion = String.Empty
151                 End If
152                 Return productVersion
153             Else
154                 Return ""
155             End If
156         End Function
157
158         ''' <summary>
159         ''' Get the name of the System from the assembly
160         ''' </summary>
161         ''' <param name="assembly"></param>
162         ''' <returns></returns>
163         Public Function GetProductName(assembly As Assembly) As String
164             If assembly IsNot Nothing Then
165                 Dim productName As String = ""
166                 Dim customAttributes As Object() = assembly.GetCustomAttributes(GetType(AssemblyProductAttribute), False)
167                 If (customAttributes IsNot NothingAndAlso (customAttributes.Length > 0) Then
168                     productName = DirectCast(customAttributes(0), AssemblyProductAttribute).Product
169                 End If
170                 If String.IsNullOrEmpty(productName) Then
171                     productName = String.Empty
172                 End If
173                 Return productName
174             Else
175                 Return ""
176             End If
177         End Function
178
179         ''' <summary>
180         ''' Get the copyRights details from the assembly
181         ''' </summary>
182         ''' <param name="assembly"></param>
183         ''' <returns></returns>
184         Public Function GetCopyRightsDetail(assembly As Assembly) As String
185             If assembly IsNot Nothing Then
186                 Dim copyrightsDetail As String = ""
187                 Dim customAttributes As Object() = assembly.GetCustomAttributes(GetType(AssemblyCopyrightAttribute), False)
188                 If (customAttributes IsNot NothingAndAlso (customAttributes.Length > 0) Then
189                     copyrightsDetail = DirectCast(customAttributes(0), AssemblyCopyrightAttribute).Copyright
190                 End If
191                 If String.IsNullOrEmpty(copyrightsDetail) Then
192                     copyrightsDetail = String.Empty
193                 End If
194                 Return copyrightsDetail
195             Else
196                 Return ""
197             End If
198         End Function
199
200         ''' <summary>
201         ''' Get the Product tile from the assembly
202         ''' </summary>
203         ''' <param name="assembly"></param>
204         ''' <returns></returns>
205         Public Function GetProductTitle(assembly As Assembly) As String
206             If assembly IsNot Nothing Then
207                 Dim productTitle As String = ""
208                 Dim customAttributes As Object() = assembly.GetCustomAttributes(GetType(AssemblyTitleAttribute), False)
209                 If (customAttributes IsNot NothingAndAlso (customAttributes.Length > 0) Then
210                     productTitle = DirectCast(customAttributes(0), AssemblyTitleAttribute).Title
211                 End If
212                 If String.IsNullOrEmpty(productTitle) Then
213                     productTitle = String.Empty
214                 End If
215                 Return productTitle
216             Else
217                 Return ""
218             End If
219         End Function
220
221         ''' <summary>
222         ''' Get the description of the product from the assembly
223         ''' </summary>
224         ''' <param name="assembly"></param>
225         ''' <returns></returns>
226         Public Function GetProductDescription(assembly As Assembly) As String
227             If assembly IsNot Nothing Then
228                 Dim productDescription As String = ""
229                 Dim customAttributes As Object() = assembly.GetCustomAttributes(GetType(AssemblyDescriptionAttribute), False)
230                 If (customAttributes IsNot NothingAndAlso (customAttributes.Length > 0) Then
231                     productDescription = DirectCast(customAttributes(0), AssemblyDescriptionAttribute).Description
232                 End If
233                 If String.IsNullOrEmpty(productDescription) Then
234                     productDescription = String.Empty
235                 End If
236                 Return productDescription
237             Else
238                 Return ""
239             End If
240         End Function
241     End Module
242 End Namespace