VS2015 の新しい VB14 言語機能 (?. 表記や文字列補間など) が本当に気に入っています。私たちの WinForms (.Net 4.5) アプリケーションでは、コードダム コンパイラを使用して動的レポート コードをオンザフライで実行しています。残念ながら、これは新しい言語機能を自動的にサポートしません。
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 codedom nuget パッケージを追加しようとしましたが、2 つの問題が発生します。
最初の問題は、コンパイラが間違った場所 \bin\debug\bin\roslyn\vbc.exe で検索されるのに対し、ビルド プロセスでは実際にはフォルダー \bin\debug\roslyn\ にコンパイラが作成されることです。ビルドサーバーの展開に失敗するため、これを解決する方法を誰か教えてもらえますか?
roslyn フォルダーの手動コピーを作成し (回避策)、codedom がそれを見つけると予想される場所に配置することは、実際の解決策ではありませんが、System.Web.HttpRequest と System.Web.HttpResponse のインポートが予想されるという 2 番目の問題が発生します。 . ただし、Web 関連のコード (winform) はコンパイルしていません。では、なぜこれを期待しているのでしょうか。