??? Visual Basic .NET ?? Visual Basic 2005 ??????? ?? ????? ?? ????????? ?????? ???? ?? ??? ???? ????

???? ?????? ???? ??????
???? ID: 304654 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
?? Microsoft Visual C# .NET ??????? ?? ??? ?? ???? ??, ?????304655.

?? ???? ????? Microsoft .NET ????????? ????? ????????? ???????? ?? ???????? ???? ??::
  • System.CodeDom.Compiler
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

.NET Framework exposes ???? ?? ?? ???? ????????? ??????? ???? ??? Visual Basic 2005 ?? Visual Basic .NET ?? ??????? ?? ?????? ??? ?? ???? ??? compiling ?????????? ??? ????? ????? ??? ??? ?????? ?? ?? ???? ??? ?? ???? ???? ??? ??? ????? ?? ??? ?? ?????? ???? ?? ?????? ???? ?? ?? ????? ??? ?? ?????? ????? ??? ????????? ?? ?? ???? ???????? ????? ????? ?? ???????? ????? ????? ?? ??? ????? ?? ??? ???? ?????? ???? ??? ??????? ?? compilation ????????? ?? ????? ?? ?? ???? ?????? ?? ????????? ???? ????

???? ???????

??? 1: ??????????

  • ????? Studio .NET ?? Visual Studio 2005
  • Visual Basic .NET ???? ??????? ?? Visual Basic 2005 ??????? ????

??? 2: ???? ??? ????????? ?????? ????

.NET Framework ICodeCompiler ??????? ???????? ???????? ?? ?????? ????? ??? VBCodeProvider ???? ?? ??????? ?? ???? ???? ?? ?? Visual Basic ??? generator ?? ??? ??????? ?? ?????? ?? ??? ????? ?????? ???? ??? ????? ????? VBCodeProvider ?? ??????? ????? ?? ?? ??? ???? ICodeCompiler ??????? ?? ??? ??? ?????? ?? ??????? ???? ?? ??? ?? ????? ???? ???

Dim codeProvider As New VBCodeProvider()
Dim icc As ICodeCompiler = codeProvider.CreateCompiler
				

???? ??? ??? ICodeCompiler ??????? ?? ??? ??? ?????? ??, ?? ??? ?? ??? ????? ?? ???? ????? ??? ?? ?????? ???? ?? ???? ???? ??? ???????? ??????? ???? ?? ??? ?? ????? ??CompilerParameters????? ???? ?? ?????? ??:
Dim parameters As New CompilerParameters()
Dim results As CompilerResults

parameters.GenerateExecutable = True
parameters.OutputAssembly = Output
results = icc.CompileAssemblyFromSource(parameters, SourceText)
				

??? ??? ?? ?? ???????? ????? ????? (?? ??? ??? ?????? ?? DLL) ?? ??????? ???? ?? ??? ????? ??? ?? ?? ????? ?? ??? ???? ??????? ?????? ???? ????? ??? ?? ??????? ????? ???? ?? ??? CompilerParameters ???????? ?? ????? ???? ??? ??? ???? ?? ???CompileAssemblyFromSource???? ???? ??????? ?????? ???? ??? ?? ???? ??? ???? ???????? ???????? ?? ????? ???, ?? ???????? ?? ?????? ???? ??? ?????? ???? ??? ?? ???? ?? ?? ??? ?? ??? ?? ???? ?? compilation ????????? ?? ????? ?? ??? ???? ?? ???? ???? ?? ?? ????? ??? ?? ????? ????CompileAssemblyFromSource, ?? ?? ????CompilerResults???????? ??? ?? ???????? ??? ??? ????????? ??????, ?????? compile ?? ????? ??? ?? ??? ?? ?????? ???
If results.Errors.Count > 0 Then
   'There were compiler errors
   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
End If

				

???? ?????? compiling ???? ???? ????? ?? compiling ?? ??? ???? ?? compile, ????? ???? ?? ?? ?? ???? ??? ?????? ?????? ?????? ?? ????? ?? ?? ?? ??? ??? ?? batch ?? ???? ???? ?? ???? ?? ???????? ????? MSDN ?????? ?? ?????? ??:
HTTP://MSDN.Microsoft.com/en-us/default.aspx

??? 3: Step-by-Step ?????

  1. ??? ??? Visual Basic .NET ?? Visual Basic 2005 Windows ????????? ?????? Form1 ???????? ??? ?? ???? ??..
  2. ??? ????? ????? ??? Form1 ??, ????? ??? ?? ??? ?????? ??? ?????:
    Option Strict On
    Imports System.CodeDom.Compiler
    
    
    Public Class Form1
        Inherits System.Windows.Forms.Form
    
    #Region " Windows Form Designer generated code "
    
        Public Sub New()
            MyBase.New()
    
            'This call is required by the Windows Form Designer.
            InitializeComponent()
    
            'Add any initialization after the InitializeComponent() call
    
        End Sub
    
        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub
        Friend WithEvents textBox1 As System.Windows.Forms.TextBox
        Friend WithEvents button1 As System.Windows.Forms.Button
        Friend WithEvents textBox2 As System.Windows.Forms.TextBox
        Friend WithEvents button2 As System.Windows.Forms.Button
    
        'Required by the Windows Form Designer
        Private components As System.ComponentModel.Container
    
        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.textBox2 = New System.Windows.Forms.TextBox()
            Me.textBox1 = New System.Windows.Forms.TextBox()
            Me.button1 = New System.Windows.Forms.Button()
            Me.button2 = New System.Windows.Forms.Button()
            Me.SuspendLayout()
            '
            'textBox2
            '
            Me.textBox2.BackColor = System.Drawing.SystemColors.Control
            Me.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.None
            Me.textBox2.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
            Me.textBox2.ForeColor = System.Drawing.SystemColors.WindowText
            Me.textBox2.Location = New System.Drawing.Point(264, 56)
            Me.textBox2.Multiline = True
            Me.textBox2.Name = "textBox2"
            Me.textBox2.Size = New System.Drawing.Size(240, 232)
            Me.textBox2.TabIndex = 2
            Me.textBox2.Text = ""
            '
            'textBox1
            '
            Me.textBox1.Location = New System.Drawing.Point(16, 48)
            Me.textBox1.Multiline = True
            Me.textBox1.Name = "textBox1"
            Me.textBox1.Size = New System.Drawing.Size(240, 240)
            Me.textBox1.TabIndex = 0
            Me.textBox1.Text = "textBox1"
            '
            'button1
            '
            Me.button1.Location = New System.Drawing.Point(368, 304)
            Me.button1.Name = "button1"
            Me.button1.TabIndex = 1
            Me.button1.Text = "Build"
            '
            'button2
            '
            Me.button2.Location = New System.Drawing.Point(456, 304)
            Me.button2.Name = "button2"
            Me.button2.TabIndex = 1
            Me.button2.Text = "Run"
            '
            'Form1
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(544, 333)
            Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.button2, Me.textBox1, Me.button1, Me.textBox2})
            Me.Name = "Form1"
            Me.Text = "Form1"
            Me.ResumeLayout(False)
    
        End Sub
    
    #End Region
    
        Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click, button2.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
    
    End Class
    					
    ???:??? Visual Basic 2005 ??? ????????? ???? ?? ???? ???? ?????? ??? ?? Visual Basic 2005 ??? Form1 ??? ?? ?? ??? ??????? ?????, ???? ??? ???? ??? ?? ??? ??? Form1.vb ????? ?? ??????? ???????? ??? ?? ??? ?????? Form1.Designer.vb ?????? Form1 ?? ??????????? ?? ?? ??? ??????? ??? ??????? ???? ?? ??? Windows ??????? ??????? ????? ?????? ?? ????? ???? ??? ?? designer emitted ??? ?? ???? ??? interspersed ?? ????? ??? Visual Basic 2005 ???? ????????????? ?? ???? ??? ???? ??????? ?? ??? ????? Microsoft ??? ???? ?? ????:
    (vs.80) http://msdn2.Microsoft.com/en-us/library/ms379584 .aspx
    ????? ???? ?? Windows ??????? ??????? ?? ???? ??? ???? ??????? ?? ??? ????? Microsoft ??? ???? ?? ????:
    HTTP://msdn2.Microsoft.com/en-us/library/ms171843.aspx
  3. ????????? ?? ?????? Form1 ??? ?? ???? ??, ?? ??? ????? ???????????? ??? ????? ??? ?? ??? ??????? ????????? ??????? ???
  4. ????, ??? ?????, ??? ?? ?????? ??? ?? ????? ??? ????? ??? ?? ????????? ?????:
    Imports System
    
    Module Module1
    
        Sub Main()
            Console.WriteLine("Hello World!")
            Console.WriteLine("Press ENTER")
            Console.ReadLine()
        End Sub
    
    End Module
    					
  5. ????? ????,????????:? Compile ??? ???? ??????
  6. ????? ????,??????? ?? ??? ?????? ???? ?? ??? ???? ???????? ????? ????? ????? ?????? Compile ????????? ??? ??? ??? ?? ??? ??????? ??? "out.exe" ???? ?? ???????? ????? ????? ????? ???

    ???::?? ???? ??????? ????????? ?? ????? ?? ??? ??? ????? ??? ??? ?? ??????? ?? ???? ???? ?????? ?? ???, ?? ??? ?????? ????? ?? ??? ?? ??? ?? ?????? ?????
  7. ?????? ????? ?? ??? ?? ????? ?????? ?? ?????? ?? ??? ??? ????? ??? ??? lastly, ??????? ????? ????? ????,??????????? ????? ???

??????

VBCodeProvider ????
HTTP://MSDN.Microsoft.com/en-us/library/Microsoft.visualbasic.vbcodeprovider.aspx

ICodeCompiler ???????
(VS.71) http://MSDN.Microsoft.com/en-us/library/SYSTEM.codedom.compiler.icodecompiler .aspx

???

???? ID: 304654 - ????? ???????: 04 ?????? 2010 - ??????: 2.0
???? ???? ???? ??:
  • Microsoft .NET Framework 1.0
  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
??????: 
kbvs2005applies kbvs2005swept kbbcl kbcompiler kbhowto kbprod2web kbmt KB304654 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:304654

??????????? ???

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com