如何在 ASP.NET Core(.NET Framework 目标)项目中引用旧程序集?

How to reference an old assembly in ASP.NET Core (.NET Framework targeting) project?

我正在构建一个 ASP.NET 核心项目(目标框架为 .NET Framework 4.5.2),在一堆 .NET Framework 项目的解决方案中。我引用的是使用 StyleCop.MsBuild 的项目之一。但是,当我执行 dotnet build 时,我得到:

C:\MySolution\nupkgs\StyleCop.MSBuild.4.7.54.0\build\StyleCop.MSBuild.Targets(101,5): error MSB4062: The "StyleCopTask" task could not be loaded from the assembly
C:\MySolution\nupkgs\StyleCop.MSBuild.4.7.54.0\build\..\tools\StyleCop.dll. Could not load file or assembly 'System.Windows.Forms, Version=2.0.0.0, 
Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified. Confirm that the <UsingTask> declaration is correct, that the assembly and all its 
dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask. [
C:\MySolution\src\MyProject\MyProject.csproj] 

其中 MyProject 是我要引用的项目。

我试过将 System.Windows.Forms 作为依赖项包括在内,但它的版本是 4.0.0.0 而不是 2.0.0.0,所以这似乎不起作用。

有没有参考 System.Windows.Forms 来构建它?

我尝试安装 Nuget 包,提示它与 .net core 不兼容。

我的猜测是 stylecop 尚未移植到 .net 核心。

.net core 和.net 不一定相互兼容。

正如@lionnnn 在他们的回答中所述,从 4.7.55 开始,StyleCop 是 not yet supported on .NET Core

我可以通过直接将项目本身所需的文件作为 link 包括在内来解决这个问题:

.csproj:

  <Compile Include="..\MyProject\MyFile.cs">
      <Link>MyProject\%(FileName)%(Extension)</Link>
  </Compile>

这将所需的文件编译到我的项目中,而没有引入所有依赖项。这不是最好的解决方案,但这种解决方法可以满足我的需求。