-1

I checked some resource about roslyn,and i not found how to compile c# sources to executable with Roslyn.I can easily compile some .cs files to .exe using CodeDom:

    /// <summary>
    /// "anycpu" || "anycpu32bitpreferred" || "x86" || "x64" || "ARM" || "Itanium"
    /// </summary>
    public static string param = "anycpu";

    public static string BCS(string[] sources,string[] libs,string outPath,bool exef)
    {
        var options = new Dictionary<string, string> {
         { "CompilerVersion", "v4.0.0" }
        };
        CSharpCodeProvider codeProvider = new CSharpCodeProvider(options);
        CompilerParameters parameters = new CompilerParameters(libs);
        parameters.GenerateExecutable = exef;
        parameters.OutputAssembly = outPath;
        parameters.CompilerOptions = "-platform:" + param;
        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";
        }
    }

but problem of CodeDom - he can compile only c# with .NET Framework 4.0,but i need to compile c# files with 4.6.1 .NET Framework version.So,question: Can i compile some c# files(.cs) with 4.6.1 .NET Framework version using Roslyn Compiler?

Byte
  • 55
  • 11
  • 1
    Maybe [this](https://josephwoodward.co.uk/2016/12/in-memory-c-sharp-compilation-using-roslyn) article can help you – MindSwipe Jan 25 '19 at 11:55
  • i gonna check it – Byte Jan 25 '19 at 12:02
  • its only to compile one source,i need to compile not one file.Also it has errors – Byte Jan 25 '19 at 12:07
  • Check out the `MsBuild` executable or the `dotnet build` shell command – MindSwipe Jan 25 '19 at 12:13
  • umm??I need to compile **not one file** and,if you remember,i writed,i need to do it in c# application at runtime. – Byte Jan 25 '19 at 12:15
  • You can execute executables from C# code, so basically you would be delegating the compilation of the Project/ Code to an established tool instead of reinventing the wheel – MindSwipe Jan 25 '19 at 12:21
  • [Here](https://stackoverflow.com/questions/9679375/run-an-exe-from-c-sharp-code) is how to start an executable from C# code and [here](https://stackoverflow.com/a/32070575/9363973) is a link to a Stack Overflow question asking how to compile C# manually (no visual studio) – MindSwipe Jan 25 '19 at 12:24

1 Answers1

1

The CodeDom has been deprecated in favor of the Roslyn APIs. On .NET Framework (ie. .NET 4.x) you can continue to use the CSharpCodeProvider which uses the built in compiler which supports up to C# 6 if memory serves. If you want to use C# versions later than that you need to use Roslyn and there's a shim CodeProvider that uses Roslyn that gives you access to later C# versions.

Here's what this looks like using either the 'classic' provider or Roslyn provider with the CodeDom:

if (CompilerMode == ScriptCompilerModes.Roslyn)
    // NuGet Package: Microsoft.CodeDom.Providers.DotNetCompilerPlatform
    _compiler = new Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider();
else
    _compiler = new Microsoft.CSharp.CSharpCodeProvider();  // classic

Ultimately though the CodeProvider interface is deprecated and it's a better idea to use the Roslyn APIs directly.

There's a lot more info both on using the old CSharpCodeProvider with Roslyn and using the Roslyn APIs in this post of mine.

Rick Strahl
  • 17,302
  • 14
  • 89
  • 134