Public Class cCompiler
#Region " 变量 "
Private _CodeLanguage As enuCodeLanguage = mdlPublic.enuCodeLanguage.Default
Private _lstErrors As ListView
Private _hasError As Boolean = False
Private _oAssembly As [Assembly]
Private _Type As Type
Private Provider As Object
Private oCompiler As ICodeCompiler
Private AsmNameString As String() = {"System.DLL", "System.Data.dll", "System.Windows.Forms.dll", "System.XML.dll", "Microsoft.VisualBasic.dll"}
Private oCompParams As CompilerParameters
#End Region
#Region " 属性 "
Friend Property CodeLanguage() As enuCodeLanguage
Get
Return _CodeLanguage
End Get
Set(ByVal Value As enuCodeLanguage)
_CodeLanguage = Value
End Set
End Property
Friend Property ListViewForErrors() As ListView
Get
Return _lstErrors
End Get
Set(ByVal Value As ListView)
_lstErrors = Value
End Set
End Property
Friend ReadOnly Property HasError() As Boolean
Get
Return _hasError
End Get
End Property
Friend ReadOnly Property OutAssembly() As [Assembly]
Get
Return _oAssembly
End Get
End Property
#End Region
#Region " 私有方法 "
Private Sub ListErrors(ByVal ErrorCollection As CompilerErrorCollection)
If _lstErrors Is Nothing Then Return
Dim i As Integer
_lstErrors.Items.Clear()
Dim pErr As CompilerError
For i = 0 To ErrorCollection.Count - 1
Dim pListItem As Windows.Forms.ListViewItem
pErr = ErrorCollection.Item(i)
If pErr.IsWarning Then
pListItem = _lstErrors.Items.Add((i + 1).ToString, 1)
Else
pListItem = _lstErrors.Items.Add((i + 1).ToString, 0)
End If
'pListItem.SubItems.Add(pErr.FileName)
pListItem.SubItems.Add(pErr.Line)
pListItem.SubItems.Add(pErr.Column)
pListItem.SubItems.Add(pErr.ErrorNumber)
pListItem.SubItems.Add(pErr.ErrorText)
Next
End Sub
Private Sub Init()
If _CodeLanguage = mdlPublic.enuCodeLanguage.CSharp Then
Provider = New Microsoft.CSharp.CSharpCodeProvider
Else
Provider = New Microsoft.VisualBasic.VBCodeProvider
End If
oCompiler = Provider.CreateCompiler()
oCompParams = New CompilerParameters(AsmNameString)
oCompParams.GenerateExecutable = False
oCompParams.GenerateInMemory = True
End Sub
#End Region
#Region " 公共方法 "
Friend Function Compiler(ByVal StrSourceCode As String) As [Assembly]
Dim oCodeUnit As CodeSnippetCompileUnit
Dim oCompResults As CompilerResults
oCodeUnit = New CodeSnippetCompileUnit(StrSourceCode)
oCompResults = oCompiler.CompileAssemblyFromDom(oCompParams, oCodeUnit) '开始编译
_oAssembly = Nothing
If oCompResults.Errors.HasErrors Then
ListErrors(oCompResults.Errors)
_hasError = True
Else
Try
_oAssembly = oCompResults.CompiledAssembly
Return _oAssembly
_hasError = False
Catch ex As Exception
'Throw ex
cMsgBox(ex.Message)
End Try
_lstErrors.Items.Clear()
End If
End Function
Friend Function Run(ByVal ParamArray strParams As String()) As Boolean
If _oAssembly Is Nothing Then Return False
Dim oType As Type
Dim sNameSpace, sTypeName As String
Dim oMeth As MethodInfo
For Each oType In _oAssembly.GetTypes
If oType.IsClass Then
'oMeth = oType.GetMethod("Main")
'If Not oMeth Is Nothing Then
sNameSpace = oType.Namespace
sTypeName = oType.Name
For Each oMeth In oType.GetMethods(BindingFlags.Public)
cMsgBox(oMeth.Name)
Next
Exit For
'End If
End If
Next
Try
If sTypeName <> "" Then
If sNameSpace <> "" Then sTypeName = sNameSpace & "." & sTypeName
Dim oObj As Object = _oAssembly.CreateInstance(sTypeName)
If strParams.Length > 0 Then oObj.main(strParams) Else oObj.main()
End If
Return True
Catch ex As Exception
Throw ex
Return False
End Try
End Function
Friend Function GetOutType(ByVal typeName As String) As Type
Try
If _oAssembly Is Nothing Then Return Nothing
Return _oAssembly.GetType(typeName)
Catch ex As Exception
Throw ex
Return Nothing
End Try
End Function
Friend Function GetOutTypes(ByVal typeName As String) As Type()
Try
If _oAssembly Is Nothing Then Return Nothing
Return _oAssembly.GetTypes
Catch ex As Exception
Throw ex
Return Nothing
End Try
End Function
Friend Function GetMethod(ByVal typeName As String, ByVal MethodName As String) As MethodInfo
Try
If _oAssembly Is Nothing Then Return Nothing
Dim oType As Type = _oAssembly.GetType(typeName)
Return oType.GetMethod("Main", BindingFlags.Public Or BindingFlags.InvokeMethod)
Catch ex As Exception
Throw ex
End Try
End Function
#End Region
#Region " 构造函数 "
Public Sub New()
Init()
End Sub
Public Sub New(ByVal CodeLanguage As enuCodeLanguage)
_CodeLanguage = CodeLanguage
Init()
End Sub
#End Region