错误 CS0579 重复 'global::System.Runtime.Versioning.TargetFrameworkAttribute'

Kir*_*eed 39 netcoreapp3.1

当我构建我的应用程序时,我收到以下错误

 Error  CS0579  Duplicate 'global::System.Runtime.Versioning.TargetFrameworkAttribute' attribute    MyUIApp
D:\MyUIApp\obj\Debug\netcoreapp3.1\.NETCoreApp,Version=v3.1.AssemblyAttributes.cs   4   Active
Run Code Online (Sandbox Code Playgroud)

obj/Debug/netcoreapp3.1文件夹下自动生成以下代码

// 使用系统;使用 System.Reflection; [程序集: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v3.1", FrameworkDisplayName = "")]

我有一个以开头的项目文件

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <OutputType>Library</OutputType>
    <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <RestorePackages>true</RestorePackages>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>
  <PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

我可以通过注释掉文件的内容来解决这个问题,但不能通过删除文件来解决。

wsa*_*oht 58

我也在 VS Code 中遇到了这个错误,以下修复了它。

我有一个项目/解决方案,其中包含三个项目。

  • 网络标准2.1
  • 网络标准2.1
  • netcoreapp3.1

我向该部分中的每个*.csproj文件添加了以下行<PropertyGroup>

<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
Run Code Online (Sandbox Code Playgroud)

完整示例

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
    <GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  </PropertyGroup>

</Project>
Run Code Online (Sandbox Code Playgroud)

后做上述可能需要清洁/bin/obj文件夹的每个项目。

这篇文章为我指明了正确的方向,尽管我在网上发现的任何内容都没有提到上面的属性。我只是猜测,它奏效了!

  • 这并没有解决问题的原因。它只是隐藏它。 (16认同)
  • 我觉得我们很多人都被这个问题绊倒的原因是微软决定对 VS 2022 中项目文件的工作方式进行根本性的改变。以前(VS 2019 及之前)项目文件列出了所有将要编译的文件。在 VS 2022 中,项目文件夹中的“所有文件”都将被编译,除非项目文件中明确排除了它们。这更像是 Eclipse 和 NetBeans 等其他 IDE 的工作方式,但我讨厌它。就我而言,我的项目的子文件夹中残留有第二个 AssemblyInfo.cs 文件。 (5认同)

小智 42

将以下两行添加到<PropertyGroup>. 这为我修好了。

<PropertyGroup>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>    
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

  • 这应该被赞成并推广为解决方案 (6认同)
  • 认为这个问题可能是设计造成的,但简单的解决办法并不明显。.Net Core 包括通过目录层次结构中的位置推断的内容...我有 ```lang-text rootdir\x.sln、rootdir\x.csproj rootdir\Proj2\proj2.csprog。``` 由于推断“proj2”自动出现在“x.csproj”中。如果可能的话,在 VS 本身中打开 sln,在解决方案资源管理器中,“从项目中排除”“AnotherProj”,完成并修复;)我会发布 csproj 实体,但会超出最大注释长度。简单从这里开始。通过推理包含文件有优点和缺点 (3认同)
  • 我并不是很清楚这个文件会进入哪个文件以及它去哪里。它们位于 &lt;PropertyGroup&gt; 标记下的 .csproj 文件中 (2认同)
  • @LongDoThanh 不,不应该。它首先无法确定导致问题的原因。也不知道为什么。这是一个复制粘贴的创可贴,掩盖了“实际”问题。 (2认同)

小智 32

所以我确实在基于 .NET 4.7 的解决方案上遇到了同样的情况,花了几个小时,才发现我的一位同事确实在项目中包含了 obj 和 bin 文件夹!排除它们解决了问题并且该错误消失了。

希望这能为某人节省几个小时。

  • 排除所有项目的 bin 和 obj 文件夹。有时,在复制粘贴项目文件夹后,它们最终会成为项目的一部分。 (2认同)

小智 23

我通过删除每个项目目录中的 obj 和 bin 文件夹来修复此问题。然后我清理了解决方案并重建。重建成功了。

  • 对我来说也是如此,只需删除以前的构建工件就可以解决问题。 (2认同)

Cla*_*doo 14

问题出在我的文件夹结构上:测试项目位于主项目文件夹中。在同一个 repo 中并排传递解决了这个问题

MyProject
   src/MyProject.csproj
   tests/MyTestProject.csproj
Run Code Online (Sandbox Code Playgroud)

取自 Github 问题:https : //github.com/dotnet/core/issues/4837

  • 其他解决方案只是隐藏了问题。 (15认同)
  • 这对我有帮助。我突然得到了 CS0579,结果发现我的 Test Harness 项目与我正在测试的项目位于同一目录中,因此已在项目中被拾取。排除测试项目为我解决了这个问题。 (4认同)
  • 这是实际的解决方案。一旦我为每个项目(所有同行)都有一个专用文件夹,这个问题就得到了解决,而无需修改我的 csproj 文件。我在 Mac M1 上使用 dotnet 7 + vscode。 (4认同)

Joe*_*ely 12

您只需obj从项目/解决方案中排除该文件夹即可。如果您不知道如何执行此操作,请将其添加到您的 .csproj 或 .vbproj 或 .whateverproj 中:

  <ItemGroup>
    <Compile Remove="obj\**" />
    <Content Remove="obj\**" />
    <EmbeddedResource Remove="obj\**" />
    <None Remove="obj\**" />
  </ItemGroup>
Run Code Online (Sandbox Code Playgroud)

  • 他建议您更新答案以包含该信息。人们不应该要求你提供必要的细节。您的完整答案也不应该散布在评论部分。请参阅文章[如何回答 Stack Overflow 上的问题](https://stackoverflow.com/help/how-to-answer) 以了解此处的预期答案。 (2认同)

小智 7

尝试从 Project 中删除 obj 文件夹,从 SolutionExplorer 而不是 WindowExplorer 中删除它。


Sib*_*enu 5

I was facing the same issue in my asp.net core 3.1 application right after I add the xUnit project to the solution. Ultimately, the main issue was because of that I selected the check box Place solution and project in the same directory as shown in the preceding image.

在此处输入图片说明

This should work in normal cases, and you will just consider this root directory as the Git repository (the .sln file and the .csproj will be in the same folder). But you will not be able to add a new project to this directory as you will get the error "Error CS0579 Duplicate 'global::System.Runtime.Versioning.TargetFrameworkAttribute'". So, to fix this error, we just have to follow the preceding steps.

  1. Create a folder with the same name in the .sln file
  2. 将所有与项目相关的文件移动到该目录
  3. .sln使用任何代码编辑器打开您的文件
  4. 编辑项目引用。
  5. 确保您的.sln文件在根目录中

这就是您的项目文件引用现在的样子。

Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApplication2", "WebApplication2\WebApplication2.csproj", "{027937D8-D0E6-45A4-8846-C2E28DA102E6}"
EndProject 

Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApplication2.Tests", "WebApplication2.Tests\WebApplication2.Tests.csproj", "{AD4C6C31-F617-4E76-985A-32B0E3104004}" 
EndProject
Run Code Online (Sandbox Code Playgroud)

就是这样。只需重新加载您的解决方案并快乐编码!