1

VB コードをコンパイルしているときに、CodeDom に問題があります。「インポート 'System.Core' で指定された名前空間または型にパブリック メンバーが含まれていないか、見つかりません。」というメッセージが表示されます。これは、コンパイルする必要があるコード内のすべてのインポートに対して繰り返されます。

コンパイラ ライブラリ:

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.dll への参照を必要とする System.Data についても同様です。など。

標準フレームワーク アセンブリ参照のリストについては、独自のプロジェクトを参照してください。プロジェクト + プロパティ、[参照] タブ。

この事故は奇妙であることに注意する必要があります。コンパイラが/noconfig オプションで呼び出されない限り、コンパイラは、すべての共通フレームワーク アセンブリの参照を既に追加している vbc.rsp ファイルを自動的に使用する必要があります。あなたのケースでそれが起こらない理由がすぐにはわかりません。

于 2013-10-05T15:49:24.767 に答える