1 | #Region "Microsoft.VisualBasic::226f2cbb19aa40906cdd0c68f2f55862, Microsoft.VisualBasic.Core\CommandLine\Reflection\RunDll.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 RunDllEntryPoint |
35 | ' |
36 | ' Constructor: (+1 Overloads) Sub New |
37 | ' Function: GetDllMethod, GetPoint |
38 | ' |
39 | ' |
40 | ' /********************************************************************************/ |
41 | |
42 | #End Region |
43 | |
44 | Imports System.Reflection |
45 | Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel |
46 | Imports Microsoft.VisualBasic.Language |
47 | |
48 | Namespace CommandLine.Reflection |
49 | |
50 | ''' <summary> |
51 | ''' |
52 | ''' </summary> |
53 | Public Class RunDllEntryPoint : Inherits [Namespace] |
54 | |
55 | ''' <summary> |
56 | ''' rundll namespace::api |
57 | ''' </summary> |
58 | ''' <param name="Name"></param> |
59 | Sub New(Name As String) |
60 | Call MyBase.New(Name, "") |
61 | End Sub |
62 | |
63 | ''' <summary> |
64 | ''' |
65 | ''' </summary> |
66 | ''' <param name="entrypoint$"></param> |
67 | ''' <returns> |
68 | ''' 假若没有api的名称的话,是默认使用一个名字为``Main``的主函数来运行的 |
69 | ''' </returns> |
70 | Public Shared Function GetPoint(entrypoint As String) As NamedValue(Of String) |
71 | Dim entry = entrypoint.GetTagValue("::", trim:=True) |
72 | |
73 | If entry.Value.StringEmpty Then |
74 | entry.Value = "Main" |
75 | End If |
76 | |
77 | Return entry |
78 | End Function |
79 | |
80 | Public Shared Function GetDllMethod(assembly As Assembly, entryPoint$) As MethodInfo |
81 | Dim entry As NamedValue(Of String) = GetPoint(entryPoint) |
82 | Dim types As Type() = GetTypesHelper(assm:=assembly) |
83 | Dim dll As Type = LinqAPI.DefaultFirst(Of Type) _ |
84 | _ |
85 | () <= From type As Type |
86 | In types |
87 | Let load = type.GetCustomAttribute(Of RunDllEntryPoint) |
88 | Let name = load?.Namespace |
89 | Where Not load Is Nothing AndAlso name.TextEquals(entry.Name) |
90 | Select type |
91 | |
92 | If dll Is Nothing Then |
93 | Return Nothing |
94 | Else |
95 | Dim matchName = Function(m As MethodInfo) |
96 | Return m.Name.TextEquals(entry.Value) |
97 | End Function |
98 | Dim method As MethodInfo = dll _ |
99 | .GetMethods(PublicShared) _ |
100 | .Where(predicate:=matchName) _ |
101 | .FirstOrDefault |
102 | |
103 | Return method |
104 | End If |
105 | End Function |
106 | End Class |
107 | |
108 | End Namespace |