Pregunta

I'm having an issue with CodeDom when I'm compiling VB code. I Get a message saying "Namespace or type specified in the Imports 'System.Core' doesn't contain any public member or cannot be found.". This gets repeated for every Imports inside the code that should be compiled.

Compiler Library:

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

Code that should be compiled:

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

I've based 90% of this on http://www.codeproject.com/Articles/5472/Compiling-NET-code-on-the-fly

¿Fue útil?

Solución

  Imports System.Core

It is not clear why that's in your source code, you don't need it. But yes, that's going to generate an error since none of the assemblies you added to params.ReferencedAssemblies has that namespace. You'd also have to add System.Core.dll. Same for System.Data, that requires a reference to System.Data.dll. Etcetera.

Look in your own project for the list of standard framework assembly references. Project + Properties, References tab.

I should note that this mishap is odd. Unless the compiler is called with the /noconfig option, it should be automatically using the vbc.rsp file which already adds references for all the common framework assemblies. I don't readily see a reason why that's not happening in your case.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top