【问题标题】:How to compile Windows Form Application source code using CodeDOM如何使用 CodeDOM 编译 Windows 窗体应用程序源代码
【发布时间】:2017-04-05 00:42:58
【问题描述】:

我需要创建一个 VB.NET 应用程序,它获取 Windows 窗体应用程序 的源代码并对其进行编译。我用这个link 作为参考。

我要创建的 Windows 窗体应用程序

Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    MsgBox("test")
End Sub

结束类

我的编译代码的 VB.NET 应用程序

Imports System.IO
Imports System.Reflection
Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports Microsoft.VisualBasic

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim codeProvider As New VBCodeProvider()
    Dim icc As ICodeCompiler = codeProvider.CreateCompiler
    Dim Output As String = "Out.exe"
    Dim ButtonObject As Button = CType(sender, Button)

    textBox2.Text = ""
    Dim parameters As New CompilerParameters()
    Dim results As CompilerResults
    'Make sure we generate an EXE, not a DLL
    parameters.GenerateExecutable = True
    parameters.OutputAssembly = Output
    results = icc.CompileAssemblyFromSource(parameters, textBox1.Text)

    If results.Errors.Count > 0 Then
        'There were compiler errors
        textBox2.ForeColor = Color.Red
        Dim CompErr As CompilerError
        For Each CompErr In results.Errors
            textBox2.Text = textBox2.Text &
            "Line number " & CompErr.Line &
            ", Error Number: " & CompErr.ErrorNumber &
            ", '" & CompErr.ErrorText & ";" &
            Environment.NewLine & Environment.NewLine
        Next
    Else
        'Successful Compile
        textBox2.ForeColor = Color.Blue
        textBox2.Text = "Success!"
        'If we clicked run then launch the EXE
        If ButtonObject.Text = "Run" Then Process.Start(Output)
    End If
End Sub

Textbox1.text 包含我提供的第一个代码。当我运行程序时,它给了我这个错误

Line number 0, Error Number: BC30420, ''Sub Main' was not found in 'Out'.;

Line number 2, Error Number: BC30002, 'Type 'EventArgs' is not defined.;

Line number 3, Error Number: BC30451, ''MsgBox' is not declared. It may be inaccessible due to its protection level.;

Line number 3, Error Number: BC32017, 'Comma, ')', or a valid expression continuation expected.;

【问题讨论】:

  • 开始一个新的 WinForms 项目。或者打开一个现有的 WinForms 项目。你在哪个部分有困难?什么版本的 Visual Studio?
  • @KenWhite 我知道如何编译项目、获取 exe 等。但是我如何编译 Windows 窗体应用程序。从它的源代码?
  • 编译正在编译。您以完全相同的方式编译它们。再一次,哪个具体部分您遇到困难?我不应该问你 20 个问题来让你弄清楚问题。
  • @Plutonix:发帖人在你上面的评论中提供了链接。
  • 我不能告诉你代码有什么问题,因为你的帖子没有代码。

标签: vb.net codedom


【解决方案1】:

嗯,你错过了很多东西。

在您将传递给您的CodeDomProviderCompilerParameters 中,您必须指定目标exe 类型,默认为“/target:exe”,这意味着控制台可执行文件(然后编译器会尝试查找 Sub Main() for Entry Point),因此您必须为 WindowsForms 项目指定“/target:winexe”。

您还必须在要编译的源代码中指定所需的Imports...否则将无法解决,对于您的源代码所需的 .NET 程序集引用也是如此,您必须指定所有它们在你的CompilerParameters

最后你应该指定主类的名称然后你就可以编译它了。

我将展示一个可以适应/替换当前源代码的工作示例:

    Public Shared ReadOnly WinFormsTemplate As String =
<a>
Imports System
Imports System.Windows.Forms

Namespace MyNamespace

    Public NotInheritable Class Form1 : Inherits Form

        Public Sub New()
            Me.StartPosition = FormStartPosition.CenterScreen
        End Sub

        Private Sub Form1_Shown(ByVal sender As Object, ByVal e As EventArgs) _
        Handles MyBase.Shown
            MessageBox.Show("VisualBasic.NET WinForms Template.")
        End Sub

    End Class

End Namespace
</a>.Value

Private Sub CompileSourceCode()

    Dim cProvider As CodeDomProvider = New VBCodeProvider
    Dim cParams As New CompilerParameters
    Dim cResult As CompilerResults
    Dim sourceCode As String = WinFormsTemplate

    With cParams
        .GenerateInMemory = False
        .GenerateExecutable = True
        .OutputAssembly = Path.Combine(My.Application.Info.DirectoryPath, ".\test.exe")
        .CompilerOptions = "/target:winexe"
        .ReferencedAssemblies.AddRange({"System.dll", "System.Windows.Forms.dll", "Microsoft.VisualBasic.dll"})
        .MainClass = "MyNamespace.Form1"
    End With

    cResult = cProvider.CompileAssemblyFromSource(cParams, sourceCode)
    cProvider.Dispose()

End Sub

【讨论】:

  • 感谢您的解释和示例,这正是我想要做的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-26
相关资源
最近更新 更多