我能够使用 VB.Net CodeDomProvider 来编译使用大多数 VB.Net 功能的源代码。我无法成功编译使用空条件运算符或多行字符串的源代码。
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 来启用这些功能,您有什么建议吗?还是在某处说不支持这些功能?