2011-04-21 8 views
0

영어로는 유감스럽게 생각합니다. 잘 못 ... 저는 어셈블리를 메모리에 컴파일해야합니다. 다른 어셈블리를 컴파일해야합니다. 하나의 단추가있는 양식이 있습니다. > 두 번째 양식을 표시 - 여기에 내가 첫 번째 양식에 Button1을 클릭하면다른 어셈블리에서 어셈블리 컴파일하기

Imports System 
Imports System.Diagnostics 
Imports System.Windows.Forms 
Imports System.Windows 
Imports Microsoft.VisualBasic 
Imports System.Threading 
Imports System.IO 
    Public Class MainClass 
Inherits MarshalByRefObject 
    Public Shared Sub Main() 
    Dim tf As New testtwo 
    application.run(tf) 
    End Sub 
End Class 
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _ 
Partial Class testtwo 
    Inherits System.Windows.Forms.Form 
    <System.Diagnostics.DebuggerNonUserCode()> _ 
    Protected Overrides Sub Dispose(ByVal disposing As Boolean) 
     Try 
      If disposing AndAlso components IsNot Nothing Then 
     components.Dispose() 
      End If 
     Finally 
      MyBase.Dispose(disposing) 
     End Try 
    End Sub 
     Private components As System.ComponentModel.IContainer 
    <System.Diagnostics.DebuggerStepThrough()> _ 
    Private Sub InitializeComponent() 
     Me.Button1 = New System.Windows.Forms.Button 
     Me.SuspendLayout() 
     Me.Button1.Location = New System.Drawing.Point(114, 40) 
     Me.Button1.Name = "Button1" 
     Me.Button1.Size = New System.Drawing.Size(231, 39) 
     Me.Button1.TabIndex = 0 
     Me.Button1.Text = "Button1" 
     Me.Button1.UseVisualStyleBackColor = True 
     Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) 
     Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font 
     Me.ClientSize = New System.Drawing.Size(465, 133) 
     Me.Controls.Add(Me.Button1) 
     Me.Name = "test" 
     Me.Text = "test" 
     Me.ResumeLayout(False) 
    End Sub 
     Friend WithEvents Button1 As System.Windows.Forms.Button 
End Class 
Public Class testtwo 
    Shared str_tb As String 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
     Call openkubik() 
    End Sub 
    Private Sub openkubik() 
     Dim th As Thread = New Thread(AddressOf sborkakub) 
     th.Start() 
    End Sub 
    Private Sub sborkakub() 
     Dim evi As System.Security.Policy.Evidence = AppDomain.CurrentDomain.Evidence 
     Dim assemblyDomain As AppDomain 
     Dim assemblyDomainSetup As AppDomainSetup = New AppDomainSetup 
     assemblyDomainSetup.ApplicationBase = System.Environment.CurrentDirectory 
     assemblyDomainSetup.DisallowBindingRedirects = False 
     assemblyDomainSetup.DisallowCodeDownload = True 
     assemblyDomainSetup.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile 
     assemblyDomainSetup.LoaderOptimization = LoaderOptimization.MultiDomainHost 
     assemblyDomain = AppDomain.CreateDomain("AssemblyDomain", evi, assemblyDomainSetup) 
     assemblyDomain.DoCallBack(AddressOf assamblykub) 
    End Sub 
    Private Shared Sub assamblykub() 
     Call createtext() 
     Dim objCodeCompiler As System.CodeDom.Compiler.CodeDomProvider = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("VB") 
     Dim objCompilerParameters As New System.CodeDom.Compiler.CompilerParameters() 
     dim asm as System.Reflection.Assembly 
    For Each asm In AppDomain.CurrentDomain.GetAssemblies() 
      objCompilerParameters.ReferencedAssemblies.Add(asm.Location) 
     Next 
     'objCompilerParameters.OutputAssembly = "res1" 
     objCompilerParameters.CompilerOptions = "/target:winexe" 
     objCompilerParameters.GenerateExecutable = True 
     objCompilerParameters.GenerateInMemory = True 
     objCompilerParameters.IncludeDebugInformation = False 
     Dim objCompileResults As System.CodeDom.Compiler.CompilerResults = objCodeCompiler.CompileAssemblyFromSource(objCompilerParameters, str_tb) 
     If objCompileResults.Errors.HasErrors Then 
      MessageBox.Show(String.Format("Error: Line>{0}, {1} # {2}", objCompileResults.Errors(0).Line, objCompileResults.Errors(0).ErrorText, objCompileResults.Errors(0).ErrorNumber)) 
      Return 
     End If 
     Dim objAssembly As System.Reflection.Assembly = objCompileResults.CompiledAssembly 
     Dim objTheClass As Object = objAssembly.CreateInstance("MainClass") 
     If objTheClass Is Nothing Then 
      MsgBox("Can't load class...") 
      Exit Sub 
     End If 
     Try 
      objTheClass.GetType.InvokeMember("Main", System.Reflection.BindingFlags.InvokeMethod, _ 
      Nothing, objTheClass, Nothing) 
     Catch ex As Exception 
      MsgBox("Error:" & ex.Message) 
     End Try 
    End Sub 
    Private Shared Sub createtext() 
     Dim tempfile As New IO.FileStream(Application.StartupPath & "/temp_open.txt", IO.FileMode.Open, IO.FileAccess.Read) 
     Dim tempfilesr As New IO.StreamReader(tempfile, System.Text.Encoding.GetEncoding(1251)) 
     str_tb = tempfilesr.ReadToEnd 
     tempfilesr.Close() 
     tempfile.Close() 
     tempfile = Nothing 
     tempfilesr = Nothing 
    End Sub 
End Class 

양식

Imports System 
Imports System.Threading 
Imports System.CodeDom 
Imports System.CodeDom.Compiler 
Imports System.Collections 
Imports System.ComponentModel 
Imports System.Diagnostics 
Imports System.Drawing 
Imports System.IO 
Imports System.Windows.Forms 
Imports Microsoft.VisualBasic 
Public Class testtwo 
    Shared str_tb As String 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
     Call openkubik() 
    End Sub 
    Private Sub openkubik() 
     Dim th As Thread = New Thread(AddressOf sborkakub) 
     th.Start() 
    End Sub 
    Private Sub sborkakub() 
     Dim evi As System.Security.Policy.Evidence = AppDomain.CurrentDomain.Evidence 
     Dim assemblyDomain As AppDomain 
     Dim assemblyDomainSetup As AppDomainSetup = New AppDomainSetup 
     assemblyDomainSetup.ApplicationBase = System.Environment.CurrentDirectory 
     assemblyDomainSetup.DisallowBindingRedirects = False 
     assemblyDomainSetup.DisallowCodeDownload = True 
     assemblyDomainSetup.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile 
     assemblyDomainSetup.LoaderOptimization = LoaderOptimization.MultiDomainHost 
     assemblyDomain = AppDomain.CreateDomain("AssemblyDomain", evi, assemblyDomainSetup) 
     assemblyDomain.DoCallBack(AddressOf assamblykub) 
     Call assamblykub() 
    End Sub 
    Private Shared Sub assamblykub() 
     Call createtext() 
     Dim objCodeCompiler As System.CodeDom.Compiler.CodeDomProvider = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("VB") 
     Dim objCompilerParameters As New System.CodeDom.Compiler.CompilerParameters() 
     For Each asm In AppDomain.CurrentDomain.GetAssemblies() 
      objCompilerParameters.ReferencedAssemblies.Add(asm.Location) 
     Next 
     objCompilerParameters.CompilerOptions = "/target:winexe" 
     objCompilerParameters.GenerateExecutable = True 
     objCompilerParameters.GenerateInMemory = True 
     objCompilerParameters.IncludeDebugInformation = False 
     Dim objCompileResults As System.CodeDom.Compiler.CompilerResults = objCodeCompiler.CompileAssemblyFromSource(objCompilerParameters, str_tb) 
     If objCompileResults.Errors.HasErrors Then 
      MessageBox.Show(String.Format("Error: Line>{0}, {1} # {2}", objCompileResults.Errors(0).Line, objCompileResults.Errors(0).ErrorText, objCompileResults.Errors(0).ErrorNumber)) 
      Return 
     End If 
     Dim objAssembly As System.Reflection.Assembly = objCompileResults.CompiledAssembly 
     Dim objTheClass As Object = objAssembly.CreateInstance("MainClass") 
     If objTheClass Is Nothing Then 
      MsgBox("Can't load class...") 
      Exit Sub 
     End If 
     Try 
      objTheClass.GetType.InvokeMember("Main", System.Reflection.BindingFlags.InvokeMethod, _ 
      Nothing, objTheClass, Nothing) 
     Catch ex As Exception 
      MsgBox("Error:" & ex.Message) 
     End Try 
    End Sub 
    Private Shared Sub createtext() 
     Dim tempfile As New IO.FileStream(Application.StartupPath & "/temp_open.txt", IO.FileMode.Open, IO.FileAccess.Read) 
     Dim tempfilesr As New IO.StreamReader(tempfile, System.Text.Encoding.GetEncoding(1251)) 
     str_tb = tempfilesr.ReadToEnd 
     tempfilesr.Close() 
     tempfile.Close() 
     tempfile = Nothing 
     tempfilesr = Nothing 
    End Sub 
End Class 

의 코드 그리고 "temp_open.txt"파일의 코드입니다. 하지만 두 번째 폼에서 Button 1을 클릭하면 FileNotFound 예외가 생깁니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 순간이 있습니다. C# (http://stackoverflow.com/questions/5729403/compilling-assembly-from-the-other-just-compilled-assembly)에서 몇 가지 변경 사항이 적용된이 예제는 어쩌면 VB와 C#을 사용하지 않고 두 예제 간의 차이점을 이해하고 있습니까?

답변

0

의 간단한 호출로 assemblyDomain.DoCallBack(AddressOf assamblykub)을 바꾸려고 했습니까?

+0

예, 저는 동일한 문제가 있습니다. 새로운 appdomain 생성을 추가 할 때가있었습니다. 예외 "vbc : 명령 줄 (0,0) : 오류 BC2006 : 'r'옵션에 ': '" –

+1

이 (가) asim asim as System.Reflection.Assembly와 일치하지 않음 각 asm에서 AppDomain.CurrentDomain.GetAssemblies) objCompilerParameters.ReferencedAssemblies.Add (asm.Location) 다음 ') 등 안톤 세메 노프에 도움을 주셔서 감사를 'objCompilerParameters.ReferencedAssemblies.Add ("System.dll을"해야 –

관련 문제