如何解决如何使用 VB.Net CodeDomProvider 编译带有空条件运算符或多行字符串的源代码
我能够使用 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 启用这些功能的文档,您有什么建议吗?还是在某处说不支持这些功能?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。