| 1 | #Region "Microsoft.VisualBasic::bba9605f99a58a17721add7e0e4d3328, Microsoft.VisualBasic.Core\ApplicationServices\Debugger\NETDebugger.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 | ' |
| 35 | ' /********************************************************************************/ |
| 36 | |
| 37 | #End Region |
| 38 | |
| 39 | 'Imports System.Reflection |
| 40 | |
| 41 | Namespace ApplicationServices.Debugging |
| 42 | |
| 43 | ' ''' <summary> |
| 44 | ' ''' .NET程序调试器 |
| 45 | ' ''' </summary> |
| 46 | ' ''' <remarks></remarks> |
| 47 | 'Public Class NETDebugger |
| 48 | |
| 49 | ' ''' <summary> |
| 50 | ' ''' |
| 51 | ' ''' </summary> |
| 52 | ' ''' <param name="AssemblyPath">.NET应用程序可执行文件或者包含有Main定义的dll文件</param> |
| 53 | ' ''' <param name="argvs">调试所使用的命令行参数</param> |
| 54 | ' ''' <returns></returns> |
| 55 | ' ''' <remarks></remarks> |
| 56 | ' Public Function Start(AssemblyPath As String, argvs As String()) As Integer |
| 57 | |
| 58 | ' AssemblyPath = FileIO.FileSystem.GetFileInfo(AssemblyPath).FullName |
| 59 | |
| 60 | ' Dim Assembly As System.Reflection.Assembly = System.Reflection.Assembly.LoadFile(AssemblyPath) |
| 61 | ' Dim GetMainEntryLQuery = (From TypeEntry As System.Reflection.TypeInfo |
| 62 | ' In Assembly.DefinedTypes |
| 63 | ' Let EntryMain As MethodInfo = GetMainEntry(TypeEntry) |
| 64 | ' Where EntryMain IsNot Nothing |
| 65 | ' Select EntryMain).ToArray |
| 66 | |
| 67 | ' If GetMainEntryLQuery.IsNullOrEmpty Then |
| 68 | ' Return -100 |
| 69 | ' End If |
| 70 | |
| 71 | ' Dim Main = GetMainEntryLQuery.First |
| 72 | ' Return RunMain(Main, argvs) |
| 73 | ' End Function |
| 74 | |
| 75 | ' ''' <summary> |
| 76 | ' ''' 函数会递归查询 |
| 77 | ' ''' </summary> |
| 78 | ' ''' <param name="TypeInfo"></param> |
| 79 | ' ''' <returns></returns> |
| 80 | ' ''' <remarks></remarks> |
| 81 | ' Public Shared Function GetMainEntry(TypeInfo As Type) As MethodInfo |
| 82 | ' Dim LQuery = (From Method As System.Reflection.MethodInfo |
| 83 | ' In TypeInfo.GetMethods(BindingFlags.Public Or BindingFlags.Static) |
| 84 | ' Where String.Equals(Method.Name, "Main", StringComparison.OrdinalIgnoreCase) AndAlso IsStandardMainReturnType(Method.ReturnType) AndAlso IsStandardMainParameterType(Method) |
| 85 | ' Select Method).ToArray |
| 86 | ' Return LQuery.FirstOrDefault |
| 87 | ' End Function |
| 88 | |
| 89 | ' Private Shared Function IsStandardMainReturnType(Type As Type) As Boolean |
| 90 | ' Return System.Type.Equals(Type, GetType(Void)) OrElse System.Type.Equals(Type, GetType(Integer)) |
| 91 | ' End Function |
| 92 | |
| 93 | ' Private Shared Function IsStandardMainParameterType(MethodInfo As MethodInfo) As Boolean |
| 94 | ' Dim pList = MethodInfo.GetParameters |
| 95 | |
| 96 | ' If pList.Count > 1 Then |
| 97 | ' Return False |
| 98 | ' ElseIf pList.IsNullOrEmpty Then |
| 99 | ' Return True |
| 100 | ' End If |
| 101 | |
| 102 | ' Dim pTypeInfo As System.Type = pList(0).ParameterType |
| 103 | ' Return pTypeInfo.Equals(GetType(String)) OrElse pTypeInfo.Equals(GetType(String())) |
| 104 | ' End Function |
| 105 | |
| 106 | ' ''' <summary> |
| 107 | ' ''' 启动Main函数 |
| 108 | ' ''' </summary> |
| 109 | ' ''' <param name="Main"></param> |
| 110 | ' ''' <param name="argvs"></param> |
| 111 | ' ''' <returns></returns> |
| 112 | ' ''' <remarks></remarks> |
| 113 | ' Private Function RunMain(Main As MethodInfo, argvs As String()) As Integer |
| 114 | |
| 115 | ' Dim pValue As Object() |
| 116 | ' Dim pInfo = Main.GetParameters |
| 117 | |
| 118 | ' If pInfo.IsNullOrEmpty Then |
| 119 | ' pValue = New Object() {} '函数没有参数 |
| 120 | ' Else |
| 121 | ' If pInfo(0).Equals(GetType(String)) Then |
| 122 | ' pValue = {argvs(0)} '字符串 |
| 123 | ' Else |
| 124 | ' pValue = {argvs} '字符串数组 |
| 125 | ' End If |
| 126 | ' End If |
| 127 | |
| 128 | ' Dim result = InternalRunFunction(Main, pValue) |
| 129 | |
| 130 | ' If Main.ReturnType.Equals(GetType(Integer)) Then |
| 131 | ' Return DirectCast(result, Integer) |
| 132 | ' Else |
| 133 | ' Return 0 |
| 134 | ' End If |
| 135 | ' End Function |
| 136 | |
| 137 | ' ''' <summary> |
| 138 | ' ''' 在终端之上打印出每一行代码 |
| 139 | ' ''' </summary> |
| 140 | ' ''' <param name="EntryPoint"></param> |
| 141 | ' ''' <param name="argvs"></param> |
| 142 | ' ''' <returns></returns> |
| 143 | ' ''' <remarks></remarks> |
| 144 | ' Private Function InternalRunFunction(EntryPoint As MethodInfo, argvs As Object()) As Object |
| 145 | |
| 146 | |
| 147 | |
| 148 | ' End Function |
| 149 | |
| 150 | 'End Class |
| 151 | End Namespace |