1

我在编译 VB 代码时遇到了 CodeDom 问题。我收到一条消息,说“在 Imports 'System.Core' 中指定的命名空间或类型不包含任何公共成员或找不到。”。对于应该编译的代码中的每个 Imports,都会重复此操作。

编译器库:

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

4

1 回答 1

1
  Imports System.Core

目前尚不清楚为什么它在您的源代码中,您不需要它。但是,是的,这会产生错误,因为您添加到 params.ReferencedAssemblies 的程序集都没有该命名空间。您还必须添加 System.Core.dll。System.Data 也一样,需要引用 System.Data.dll。等等。

在您自己的项目中查找标准框架程序集参考列表。项目 + 属性,参考选项卡。

我应该指出,这个事故很奇怪。除非使用/noconfig 选项调用编译器,否则它应该自动使用 vbc.rsp 文件,该文件已经添加了对所有常见框架程序集的引用。我看不出你的情况没有发生这种情况的原因。

于 2013-10-05T15:49:24.767 回答