在 C# 中,我可以使用下面的代码编译 VB 和 C# 代码,但我无法编译 C/C++ 代码。有没有办法做到这一点?
C# 编译器:
public void Compile(string ToCompile)
{
string Result = null;
string errors = null;
Microsoft.CSharp.CSharpCodeProvider codeProvider = new Microsoft.CSharp.CSharpCodeProvider();
System.CodeDom.Compiler.ICodeCompiler icc = codeProvider.CreateCompiler();
string Output = @"mypath";
System.CodeDom.Compiler.CompilerParameters parameters = new System.CodeDom.Compiler.CompilerParameters();
parameters.GenerateExecutable = true;
parameters.OutputAssembly = Output;
System.CodeDom.Compiler.CompilerResults results = icc.CompileAssemblyFromSource(parameters, ToCompile);
if (ReturnErrors == true)
{
if (results.Errors.Count > 0)
{
foreach (System.CodeDom.Compiler.CompilerError CompErr in results.Errors)
{
errors +=
"Line number " + CompErr.Line +
", Error Number: " + CompErr.ErrorNumber +
", '" + CompErr.ErrorText + ";" +
Environment.NewLine + Environment.NewLine;
}
Result += "Errors have been found in your code: " + Environment.NewLine + errors;
}
else
{
Result += "Success!";
System.Diagnostics.Process.Start(Output);
}
}
要创建一个 VB 编译器,我只需替换Microsoft.CSharp.CSharpCodeProvider codeProvider = new Microsoft.CSharp.CSharpCodeProvider();
为Microsoft.VisualBasic.VBCodeProvider codeProvider = new Microsoft.VisualBasic.VBCodeProvider();