简体   繁体   中英

C# - Compile c# code at runtime with custom configuration

I has a question, does CodeDom Compiler can compile c# code with custom configuration such as x64 bit or x86 bit.By default it compiles c# code to .exe with "Any CPU" configuration. Compiling c# code:

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";
        }
    }

I think,youre understand my question,if not, leave a comment,i will give details.

Try to set CompilerOptions this way

parameters.CompilerOptions = "-platform:anycpu32bitpreferred";

using params from this link

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/platform-compiler-option

PS CSharpCodeProvider uses csc.exe

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/command-line-building-with-csc-exe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM