0

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.

Byte
  • 55
  • 11
  • Poorly written, can you please explain what is your error message? if you want to know can CodeDom be compile C# the answer is yes it can please refer to https://learn.microsoft.com/en-us/dotnet/framework/reflection-and-codedom/using-the-codedom – Waheed Rafiq Jan 24 '19 at 16:58
  • I dont have any errors.I just need a way to compile(to .exe) c# code for specific platforms such as x86 bit and x64 bit using CodeDom. – Byte Jan 24 '19 at 17:02

1 Answers1

0

Try to set CompilerOptions this way

parameters.CompilerOptions = "-platform:anycpu32bitpreferred";

using params from this link

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

P.S. CSharpCodeProvider uses csc.exe

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

Woldemar89
  • 682
  • 4
  • 10
  • Also,is u know,how to compile c# with custom .NET Framework version? Like,i want to build c# with .NET Framework 4.6.1 – Byte Jan 24 '19 at 17:41
  • 1
    https://stackoverflow.com/a/9591795/10496806 `var options = new Dictionary { { "CompilerVersion", "v4.0" } }; var compiler = new CSharpCodeProvider(options);` but I'm not sure it is possible to point 4.6.1 as you want, here is compiler version, it differs from .net version https://stackoverflow.com/questions/13253967/how-to-target-net-4-5-with-csharpcodeprovider – Woldemar89 Jan 24 '19 at 17:43
  • Oh no! I has a error on CompilerResults line: System.InvalidOperationException: "Cannot find executable file of csc.exe compiler." – Byte Jan 24 '19 at 17:51
  • what "CompilerVersion" you have pointed? probably it is wrong or this version is not installed on your PC and.... yes, `CSharpCodeProvider` uses `csc.exe` https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/command-line-building-with-csc-exe – Woldemar89 Jan 24 '19 at 17:51
  • 4.6.1, i have compiler for that version on pc – Byte Jan 24 '19 at 17:55
  • 1
    please read more attentively my 2nd link in my 2nd comment https://stackoverflow.com/a/13255462/10496806 – Woldemar89 Jan 24 '19 at 17:56
  • so its impossible? – Byte Jan 24 '19 at 18:00
  • I think it is possible, but requires little more researches and\or understanding how it works – Woldemar89 Jan 24 '19 at 18:03
  • i just got c# library that uses .net framework 4.6.1, and i need to use it in my c# compiled app...so hard... – Byte Jan 24 '19 at 18:07
  • i did some research,and found where is 4.6.1 version,so its in `C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework` – Byte Jan 24 '19 at 18:17
  • hope it will give you direction, I'm not ready to continue discussion, sorry. As you can see simple call of `csc.exe` gives crazy output about all of the versions at whole https://imgur.com/a/nSqwQym – Woldemar89 Jan 24 '19 at 18:30