我有一个问题,CodeDom编译器是否可以使用自定义配置(如x64位或x86位)编译C#代码。默认情况下,它将 c# 代码编译为使用"任何 CPU"配置.exe。编译 C# 代码:
public static string BCS(string[] sources,string[] libs,string outPath,bool exef)
{
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters(libs);
parameters.GenerateExecutable = exef;
parameters.OutputAssembly = outPath;
CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, sources);
if (results.Errors.Count > 0)
{
string errsText = "";
foreach (CompilerError CompErr in results.Errors)
{
errsText = "("+CompErr.ErrorNumber +
")Line " + CompErr.Line +
",Column "+CompErr.Column +
":"+CompErr.ErrorText + "" +
Environment.NewLine;
}
return errsText;
}
else
{
return "Success";
}
}
我想,你明白我的问题,如果没有,请发表评论,我会给出细节。
这种方式设置CompilerOptions
parameters.CompilerOptions = "-platform:anycpu32bitpreferred";
使用此链接中的参数
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/platform-compiler-option
附言 CSharpCodeProvider
使用csc.exe
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/command-line-building-with-csc-exe