我真的很喜欢VS2015中的新VB14语言功能(比如?。符号和字符串插值)。在我们的WinForms(.Net 4.5)应用程序中,我使用encodeom编译器动态运行动态报告代码。不幸的是,这并不会自动支持新的语言功能。
Imports System.CodeDom.Compiler
Namespace MAF.DB.BusinessObject.ReportService
Public Class VBReportCompiler
Implements IReportCompiler
Private ReadOnly ApplicationPath As String
''' <summary>
''' Create a report compiler with a reference to the location of the Wingman dlls. These need
''' to be referenced in the compilation, but the location differs between web and windows.
''' </summary>
''' <param name="ApplicationPath">Path to the binary folder</param>
Public Sub New(ApplicationPath As String)
Me.ApplicationPath = ApplicationPath
End Sub
Public Function Compile(convertedSourceCode As String, Optional ByRef reporter As IValidationReporter = Nothing,
Optional report As IBusinessObject = Nothing) As Object Implements IReportCompiler.Compile
If reporter Is Nothing Then reporter = IOC.Dependency.Resolve(Of IValidationReporter)()
Dim result As Object = Nothing
Try
If convertedSourceCode Is Nothing Then
reporter.Add(report, "Report definition missing or blank")
Else
Dim params As New CompilerParameters With {
.GenerateExecutable = False, ' Generate a DLL, not and EXE executable.
.GenerateInMemory = True, ' Generate the assembly in memory, don't save to a file
.IncludeDebugInformation = True
}
params.TempFiles.KeepFiles = False ' don't keep temporary source files.
' Add a reference to necessary strong-named assemblies.
With params.ReferencedAssemblies
.Add("Microsoft.VisualBasic.dll")
.Add("System.dll")
.Add("System.Core.dll")
.Add("System.Data.dll") 'Allows use in report of ADO data types
.Add("System.Data.DataSetExtensions.dll") 'Allows LINQ on DataTables
.Add("System.Drawing.dll") 'Allows access in report of GDI+ basic graphics functionality such as datatable
.Add("System.Xml.dll")
'Add References to required Wingman assemblies
.Add(IO.Path.Combine(ApplicationPath, "MAF.DB.dll")) 'Allows use in report of Data Access & Business Object functions and properties
.Add(IO.Path.Combine(ApplicationPath, "MAF.SharedClasses.dll"))
.Add(IO.Path.Combine(ApplicationPath, "DB.Foundation.dll"))
.Add(IO.Path.Combine(ApplicationPath, "MAF.Tracer.dll")) ' allows use in report of standard error messages
End With
' Create the VB compiler.
Dim provider = New VBCodeProvider() 'Use the current .Net Framework
Dim compRes = provider.CompileAssemblyFromSource(params, convertedSourceCode)
' Check whether we have any compile errors.
For Each compileError As CompilerError In compRes.Errors
reporter.Add(report, String.Format("Line {0}: {1}", compileError.Line, compileError.ErrorText))
Next
If reporter.Valid Then result = compRes.CompiledAssembly.CreateInstance("ReportCompiler")
End If
Catch ex As Exception
reporter.Add(report, "Unknown Error: " & ex.Message)
End Try
If Not reporter.Valid Then Return Nothing
Return result
End Function
End Class
为了让它工作,我试图添加Roslyn编码的nuget包,但这会产生两个问题。
第一个问题是它在错误的位置\ bin \ debug \ bin \ roslyn \ _vbc.exe中查找编译器,而构建过程实际上是在文件夹\ bin \ debug \ roslyn \中创建它。任何人都可以告诉我如何解决这个问题,因为我的构建服务器部署失败了
制作roslyn文件夹的手动副本(解决方法)并将其放在encodeom期望找到它的位置并不是一个真正的解决方案,但这引发了第二个问题,即它期望导入System.Web.HttpRequest和System。 Web.HttpResponse。但我没有编译任何与网络相关的代码(winforms)。那么为什么期待呢?
答案 0 :(得分:0)
没有任何承诺,但我有一个类似的问题,通过在website.vbproj中包含这些行(我正在升级 - 作为现有项目)来修复。他们应该被nuget包括在内......
<Import Project="..\Solution\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props" Condition="Exists('..\Solution\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" />
<Import Project="..\Solution\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props" Condition="Exists('..\Solution\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props')" />