VB.Net CodeDomProvider を使用して、VB.Net のほとんどの機能を使用するソース コードをコンパイルできます。null 条件演算子または複数行の文字列を使用するソース コードをコンパイルできません。
Sub Main()
MsgBox(System.String.Empty?.Length, , "Test null-conditional operator")
MsgBox("Muli-line
String", , "Test multi-line string")
For TestCounter = 1 To 3
Dim strCOMMENT_LINE2 = If(TestCounter = 2, "", "'")
Dim strCOMMENT_LINE3 = If(TestCounter = 3, "", "'")
Dim ret_message = New System.Text.StringBuilder
Dim ur_provider = "VisualBasic"
Dim ur_code_text = New System.Text.StringBuilder
ur_code_text.AppendLine("NameSpace TestNameSpace")
ur_code_text.AppendLine("Class TestClass" & TestCounter)
ur_code_text.AppendLine("Public Shared Function TestFunction() As String")
ur_code_text.AppendLine("Return ""HI""")
ur_code_text.Append(strCOMMENT_LINE2).AppendLine("Dim abc = System.String.Empty?.Length")
ur_code_text.Append(strCOMMENT_LINE3).AppendLine("Dim def = ""Multi-line").Append(strCOMMENT_LINE3).AppendLine("String""")
ur_code_text.AppendLine("End Function")
ur_code_text.AppendLine("End Class")
ur_code_text.AppendLine("End NameSpace")
Dim ur_ns_class_path = "TestNameSpace.TestClass" & TestCounter
Dim ur_fn_name = "TestFunction"
Dim ur_dll_list As String() = {}
Dim message_written = False
Dim objMETHOD_MAIN As System.Reflection.MethodInfo = Nothing
Dim objCOMPILER_PARM = New System.CodeDom.Compiler.CompilerParameters
objCOMPILER_PARM.GenerateInMemory = True
objCOMPILER_PARM.GenerateExecutable = False
For Each strENTRY In ur_dll_list
objCOMPILER_PARM.ReferencedAssemblies.Add(strENTRY)
Next
Dim sdaPROVIDER_OPTIONS = New System.Collections.Generic.Dictionary(Of String, String)
sdaPROVIDER_OPTIONS.Add("CompilerVersion", "v4.0")
Dim objPROVIDER = System.CodeDom.Compiler.CodeDomProvider.CreateProvider(ur_provider, sdaPROVIDER_OPTIONS)
Dim objCOMPILER_RESULT = objPROVIDER.CompileAssemblyFromSource(objCOMPILER_PARM, ur_code_text.ToString)
If objCOMPILER_RESULT.Errors.Count > 0 Then
message_written = True
ret_message.AppendLine("Compile Errors")
ret_message.AppendLine()
For Each errItem As System.CodeDom.Compiler.CompilerError In objCOMPILER_RESULT.Errors
ret_message.AppendLine(errItem.ErrorText & " [" & errItem.Line + 5 & "]")
ret_message.AppendLine()
ret_message.AppendLine()
Next errItem
ret_message.AppendLine(ur_code_text.ToString)
End If
If message_written = False Then
Dim objASSEMBLY = objCOMPILER_RESULT.CompiledAssembly
Dim t As System.Type = objASSEMBLY.CreateInstance(ur_ns_class_path).GetType()
For Each m As System.Reflection.MethodInfo In t.GetMethods
If m.Name = ur_fn_name Then
objMETHOD_MAIN = m
Exit For
End If
Next m
If objMETHOD_MAIN Is Nothing Then
message_written = True
ret_message.AppendLine("Compiled assembly does not contain ").Append(ur_ns_class_path).Append(" method: ").Append(ur_fn_name)
End If
End If
If message_written = False Then
Dim objRESULT = objMETHOD_MAIN.Invoke(Nothing, Nothing)
If objRESULT IsNot Nothing Then
ret_message.AppendLine(objRESULT.ToString)
End If
End If
MsgBox(ret_message.ToString, , "Test " & TestCounter)
Next TestCounter
End Sub 'Main
テスト 2 とテスト 3 の追加の ur_code_text 行によって、プログラムのコンパイルが停止することはありません。それらは有効なステートメントです。
VB.Net CodeDomProvider を使用してこれらの機能を有効にする方法に関するドキュメントがどこで見つかるかについて、何か提案はありますか? それとも、これらの機能はサポートされないとどこかに書いてありますか?