codedom roslyn compilation error

项目是.net4.7.2的,

 

codedom的nuget包下面

Microsoft.CodeDom.Providers.DotNetCompilerPlatform.2.0.1\tools有3个子文件夹

net45文件夹是安装和卸载的脚本

Roslyn45是4.5的版本,不支持高级语法

RoslynLatest是最新的版本,支持高级语法

 

之前手动复制了Roslyn45,出现编译错误,因为不支持C#的新语法

 

Could not find a part of the path … bin\roslyn\csc.exe

回答1

The problem with the default VS2015 templates is that the compiler isn't actually copied to the tfr\bin\roslyn\ directory, but rather the {outdir}\roslyn\ directory

Add this code in your .csproj file:

<Target Name="CopyRoslynFiles" AfterTargets="AfterBuild" Condition="!$(Disable_CopyWebApplication) And '$(OutDir)' != '$(OutputPath)'">
    <ItemGroup>
      <RoslynFiles Include="$(CscToolPath)\*" />
    </ItemGroup>
    <MakeDir Directories="$(WebProjectOutputDir)\bin\roslyn" />
    <Copy SourceFiles="@(RoslynFiles)" DestinationFolder="$(WebProjectOutputDir)\bin\roslyn" SkipUnchangedFiles="true" Retries="$(CopyRetryCount)" RetryDelayMilliseconds="$(CopyRetryDelayMilliseconds)" />
</Target>

 

回答2

TL; DR

run this in the Package Manager Console:   参数r应该是reinstall的意思

Update-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform -r

Update-Package -reinstall Microsoft.CodeDom.Providers.DotNetCompilerPlatform

More information

This problem is not related to Visual Studio itself, so answers suggesting adding build steps to copy files over are rather a workaround. Same with adding compiler binaries manually to the project.

The Roslyn compiler comes from a NuGet package and there is/was a bug in some versions of that package (I don't know exactly which ones). The solution is to reinstall/upgrade that package to a bug-free version. Originally before I wrote the answer back in 2015 I fixed it by installing following packages at specific versions:

  • Microsoft.Net.Compilers 1.1.1
  • Microsoft.CodeDom.Providers.DotNetCompilerPlatform 1.0.1

Then I looked into .csproj and made sure that the paths to packages are correct (in my case ..\..\packages\*.*) inside tags <ImportProject> on top and in <Target> with name "EnsureNuGetPackageBuildImports" on the bottom. This is on MVC 5 and .NET Framework 4.5.2.

 

 

Update-Package Autofac -Version 5.1.4 -Source https://api.nuget.org/v3/index.json
Update-Package Autofac.Extras.DynamicProxy -Version 5.0.0 -Source https://api.nuget.org/v3/index.json

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(228)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2019-10-21 Check if List<Int32> values are consecutive
2019-10-21 comparison of truncate vs delete in mysql/sqlserver
2016-10-21 Group By
2016-10-21 The property 'RowId' is part of the object's key information and cannot be modified.
2016-10-21 HashCheck
2016-10-21 File Checksum Integrity Verifier
2015-10-21 Weak Event Patterns
点击右上角即可分享
微信分享提示