Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

.net - Using C# 6 features with CodeDomProvider (Roslyn)

CodeDomProvider objCodeCompiler = CodeDomProvider.CreateProvider( "CSharp" );

CompilerParameters objCompilerParameters = new CompilerParameters();

...

CompilerResults objCompileResults = objCodeCompiler.CompileAssemblyFromFile( objCompilerParameters, files.ToArray() );

When I compile my files I get:

FileFunctions.cs(347): Error: Unexpected character '$'

Does anyone know how to get string interpolation working with CodeDom compiling?

I found this link: How to target .net 4.5 with CSharpCodeProvider?

So I tried:

     var providerOptions = new Dictionary<string, string>();
     providerOptions.Add( "CompilerVersion", "v4.0" );

     // Instantiate the compiler.
     CodeDomProvider objCodeCompiler = CodeDomProvider.CreateProvider( "CSharp", providerOptions );

But I still get the same error.

I also updated the target framework to .NET Framework 4.6.

NOTE: I can't specify "v4.5" or "v4.6" or I will get:

************** Exception Text **************
System.InvalidOperationException: Compiler executable file csc.exe cannot be found.
   at System.CodeDom.Compiler.RedistVersionInfo.GetCompilerPath(IDictionary`2 provOptions, String compilerExecutable)
   at Microsoft.CSharp.CSharpCodeGenerator.FromFileBatch(CompilerParameters options, String[] fileNames)
   at Microsoft.CSharp.CSharpCodeGenerator.System.CodeDom.Compiler.ICodeCompiler.CompileAssemblyFromFileBatch(CompilerParameters options, String[] fileNames)
   at System.CodeDom.Compiler.CodeDomProvider.CompileAssemblyFromFile(CompilerParameters options, String[] fileNames)
   at Dynamic.CodeDOMCompiler.CompileAllCodeFiles() in C:UsersDerek.MorinDocumentsVisual Studio 2010ProjectsScriptCodeScriptCode.ConvertedToC#CoreCodeDOMCompiler.cs:line 93
   at NewForm.InitializeSystem() in C:UsersDerek.MorinDocumentsVisual Studio 2010ProjectsScriptCodeScriptCode.ConvertedToC#NewForm.cs:line 179
   at NewForm.NewForm_Load(Object sender, EventArgs e) in C:UsersDerek.MorinDocumentsVisual Studio 2010ProjectsScriptCodeScriptCode.ConvertedToC#NewForm.cs:line 111
   at System.Windows.Forms.Form.OnLoad(EventArgs e)

I have tried using the suggestion by Thomas Levesque:

CodeDomProvider objCodeCompiler = new Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider();

But then I get:

************** Exception Text **************
System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:UsersDerek.MorinDocumentsVisual Studio 2010ProjectsScriptCodeScriptCode.ConvertedToC#inx86Debugin
oslyncsc.exe'.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
   at Microsoft.CodeDom.Providers.DotNetCompilerPlatform.Compiler.get_CompilerName()
   at Microsoft.CodeDom.Providers.DotNetCompilerPlatform.Compiler.FromFileBatch(CompilerParameters options, String[] fileNames)
   at Microsoft.CodeDom.Providers.DotNetCompilerPlatform.Compiler.CompileAssemblyFromFileBatch(CompilerParameters options, String[] fileNames)
   at System.CodeDom.Compiler.CodeDomProvider.CompileAssemblyFromFile(CompilerParameters options, String[] fileNames)
   at Dynamic.CodeDOMCompiler.CompileAllCodeFiles() in C:UsersDerek.MorinDocumentsVisual Studio 2010ProjectsScriptCodeScriptCode.ConvertedToC#CoreCodeDOMCompiler.cs:line 87
   at NewForm.InitializeSystem() in C:UsersDerek.MorinDocumentsVisual Studio 2010ProjectsScriptCodeScriptCode.ConvertedToC#NewForm.cs:line 179
   at NewForm.NewForm_Load(Object sender, EventArgs e) in C:UsersDerek.MorinDocumentsVisual Studio 2010ProjectsScriptCodeScriptCode.ConvertedToC#NewForm.cs:line 111
   at System.Windows.Forms.Form.OnLoad(EventArgs e)

I'm not sure why it is trying to look for "csc.exe" in a subfolder of my bin directory.

This path exists:

C:UsersDerek.MorinDocumentsVisual Studio 2010ProjectsScriptCodeScriptCode.ConvertedToC#inx86Debug oslyn

But it was looking for:

C:UsersDerek.MorinDocumentsVisual Studio 2010ProjectsScriptCodeScriptCode.ConvertedToC#inx86Debugin oslyncsc.exe

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Update: March 2018

Word of caution, NuGet version 1.0.6 ... 1.0.8 will not copy the /roslyn folder to the build output directory on non-web projects. Best stick with 1.0.5 https://github.com/aspnet/RoslynCodeDomProvider/issues/38

Run-time compilation using C#6 features requires a new compiler, as @thomas-levesque mentioned. This compiler can be installed by using the nuget package Microsoft.CodeDom.Providers.DotNetCompilerPlatform.

For desktop applications, there's a problem. The ASP.NET team, in their infinite wisdom have hard-coded the path to the compiler as <runtime-directory>in oslyncsc.exe See discussion at https://github.com/dotnet/roslyn/issues/9483

If your desktop application is compiled to myappapp.exe, the roslyn compiler will be located at myapp oslyncsc.exe, BUT THE CSharpCodeProvider WILL RESOLVE csc.exe as myappin oslyncsc.exe

As far as I can tell, you have two options

  1. Create a post-build and/or installation routine that will move the oslyn subdirectory to in oslyn.
  2. Fix the runtime code through reflection black magic.

Here is #2, by exposing the CSharpCodeProvider as a property in a utility class.

using System.Reflection;
using Microsoft.CodeDom.Providers.DotNetCompilerPlatform;

static Lazy<CSharpCodeProvider> CodeProvider { get; } = new Lazy<CSharpCodeProvider>(() => {
    var csc = new CSharpCodeProvider();
    var settings = csc
        .GetType()
        .GetField("_compilerSettings", BindingFlags.Instance | BindingFlags.NonPublic)
        .GetValue(csc);

    var path = settings
        .GetType()
        .GetField("_compilerFullPath", BindingFlags.Instance | BindingFlags.NonPublic);

    path.SetValue(settings, ((string)path.GetValue(settings)).Replace(@"bin
oslyn", @"roslyn"));

    return csc;
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...