2013-10-05 84 views
1

我在編譯VB代碼時遇到了CodeDom的問題。我得到一條消息,說:「進口'System.Core'中指定的命名空間或類型不包含任何公共成員或無法找到。」對於應該編譯的代碼中的每個Imports重複這個操作。VB.NET CodeDom問題

編譯器庫:應編制

Public Class Script 
    Private code As String 
    Private results As CompilerResults 
    Private compiled = False 
    Private engine = 1 

    Sub setCode(ByVal code As String) 
     Me.code = code 
     compiled = False 
    End Sub 
    Sub setCompiler(ByVal cid As Integer) 
     If cid > 2 Or cid < 1 Then 
      MsgBox("Invalid compiler ID given. Script being ignored...") 
     Else 
      engine = cid 
     End If 
    End Sub 
    Sub compile() 
     'Dim codeProvider As Object 
     Dim codeProvider As New VBCodeProvider() 
     'If engine = 1 Then 
     'codeProvider = New CSharpCodeProvider() 
     'Me.code = My.Resources.corecs.ToString() + Me.code 
     'ElseIf engine = 2 Then 
     'codeProvider = New VBCodeProvider() 
     'Me.code = My.Resources.corevb.ToString() + Me.code 
     ' End If 
     Dim params As New CompilerParameters() 
     params.GenerateInMemory = True 
     params.TreatWarningsAsErrors = False 
     params.WarningLevel = 4 
     Dim refs() As String = {"System.dll", "Microsoft.VisualBasic.dll"} 
     params.ReferencedAssemblies.AddRange(refs) 
     results = codeProvider.CompileAssemblyFromSource(params, Me.code) 
     If results.Errors.Count > 0 Then 
      MsgBox("Compiler error...") 
      For Each errmsg As CompilerError In results.Errors 
       MsgBox(errmsg.ErrorText) 
      Next 
      compiled = False 
     Else 
      compiled = True 
     End If 
    End Sub 

    Sub runCode(ByVal entrypoint As String) 
     Dim mAssembly As System.Reflection.Assembly 
     If results.Errors.Count = 0 Then 
      mAssembly = results.CompiledAssembly 
     Else 
      Exit Sub 
     End If 
     Dim scriptType As Type 
     Dim rslt As Object 
     Try 
      scriptType = mAssembly.GetType("Script") 
      rslt = scriptType.InvokeMember(entrypoint, System.Reflection.BindingFlags.InvokeMethod Or System.Reflection.BindingFlags.Public Or System.Reflection.BindingFlags.Static, Nothing, Nothing, Nothing) 
     Catch ex As Exception 
      MsgBox(ex) 
     End Try 
    End Sub 
End Class 

代碼:

Imports System 
Imports System.Core 
Imports System.Data 
Imports System.Data.DataSetExtensions 
Imports System.Drawing 
Imports System.Windows.Forms 
Imports System.Xml 
Imports System.Xml.Linq 
Public Class Script 
    Public Shared Sub Main() 
     MessageBox.Show("Hello") 
    End Sub 
End Class 

我已經根據90%的這對http://www.codeproject.com/Articles/5472/Compiling-NET-code-on-the-fly

回答

1
Imports System.Core 

目前尚不清楚爲什麼這是在你的源代碼中,你不需要它。但是,是的,這會產生一個錯誤,因爲您添加到params.ReferencedAssemblies的程序集都沒有該名稱空間。你還必須添加System.Core.dll。相同的System.Data,需要對System.Data.dll的引用。等等。

在您自己的項目中查找標準框架組件參考列表。項目+屬性,參考選項卡。

我應該注意到,這種不幸事實很奇怪。除非使用/noconfig option調用編譯器,否則它應該自動使用vbc.rsp文件,該文件已經爲所有常用框架程序集添加了引用。我不明白爲什麼你的情況不會發生這種情況。

+0

實際上,即使程序集「System.Core」在名稱空間System.Core中也不包含任何內容。所以這個錯誤非常有意義。 – svick