我有一个运行到.net 4.5.2的vb.net项目创建了一个c#源我希望构建在核心2.0环境中运行。我使用CodeDom如下:
Public Function Compila(sourceName As String, Source As String) As Boolean
Dim provider As CodeDomProvider = Nothing
Dim compileOk As Boolean = True
Dim path As String = getSetting("EP").ToString
provider = CodeDomProvider.CreateProvider("CSharp")
Dim exeName As String = String.Format("{0}\{1}.dll", path, sourceName.Replace(".", "_"))
Dim cp As CompilerParameters = New CompilerParameters()
cp.GenerateExecutable = False ' Generate an executable instead of a class library.
cp.OutputAssembly = exeName ' Specify the assembly file name to generate.
cp.GenerateInMemory = False ' Save the assembly as a physical file.
cp.TreatWarningsAsErrors = False ' Set whether to treat all warnings as errors.
cp.ReferencedAssemblies.Add("System.dll")
cp.ReferencedAssemblies.Add("System.Xml.dll")
cp.ReferencedAssemblies.Add("System.Data.dll")
cp.ReferencedAssemblies.Add("System.Core.dll")
cp.ReferencedAssemblies.Add("System.Xml.Linq.dll")
cp.ReferencedAssemblies.Add("System.Configuration.dll")
cp.ReferencedAssemblies.Add("System.Web.dll")
cp.ReferencedAssemblies.Add("Newtonsoft.Json.dll")
cp.ReferencedAssemblies.Add("MongoDB.Bson.dll")
cp.ReferencedAssemblies.Add("MongoDB.Driver.Core.dll")
cp.ReferencedAssemblies.Add("MongoDB.Driver.dll")
cp.ReferencedAssemblies.Add("MongoDB.Driver.Legacy.dll")
cp.ReferencedAssemblies.Add("log4net.dll")
' Invoke compilation of the source file.
Dim cr As CompilerResults = provider.CompileAssemblyFromSource(cp, Source)
If cr.Errors.Count > 0 Then
compileOk = False
' Display compilation errors.
Dim errori As String = ""
Dim ce As CompilerError
For Each ce In cr.Errors
errori += ce.ToString() + Environment.NewLine
Next ce
MessageBox.Show(errori)
Else
' Display a successful compilation message.
MessageBox.Show(sourceName + ".dll created in " + path)
End If
Return compileOk
End Function
我收到了
CS0012错误:类型'System.Object'是在程序集中定义的 不是参考。添加对程序集的引用&System; Run.Runtime,Version = 4.2.0.0 ....
等等。