首页 > 解决方案 > 有没有办法在没有 nuget 包的情况下使用 Microsoft.CodeDom.Providers.DotNetCompilerPlatform?

问题描述

我编写了一个 Asp.Net Webforms 应用程序 (aspx)。该应用程序的目标是 DotNet 4.72。在应用程序中,我使用元组,因此为了使语法正常工作,我安装了块包 Microsoft.CodeDom.Providers.DotNetCompilerPlatform 版本 2.0.0

不起作用的代码如下所示:

public (Person, string, Int) Foo() 
   {
      return (new Person(), "Hello", 42);
   }

有人告诉我用程序集引用替换 nugget 包,但我找不到让它工作的方法。我做的步骤是:

  1. 保存文件:

将包复制到项目中的文件夹中。

  1. 使用 NuGet 包管理器删除了块包。

结果:我得到语法的构建错误:

error CS1031: Type expected
error CS1519: Invalid token '(' in class, struct, or interface member declaration
error CS1519: Invalid token ',' in class, struct, or interface member declaration
error CS1519: Invalid token ',' in class, struct, or interface member declaration
error CS1519: Invalid token ')' in class, struct, or interface member declaration
error CS1520: Method must have a return type
  1. 在 web.config 文件中添加了以下内容:

<system.codedom>
  <compilers>
    <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4"
      compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
    <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
  </compilers>
</system.codedom>

然后我得到一个编译错误:

Could not find a part of the path 'C:\Users\myUserName\source\repos\WebSite1\WebSite1\bin\roslyn\csc.exe'.              

我可以在不使用 NuGet 包的情况下使用这个包吗?如果是这样,我该怎么做?

提前致谢。

标签: c#asp.netwebformsnugettuples

解决方案


尝试将 dll 文件复制到 bin 目录或在项目中创建一个 lib 文件夹并从那里引用它。还要确保右键单击dll文件->“属性-->复制到输出目录-->始终复制”被选中


推荐阅读