You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

未修改代码却突发CS0579重复属性错误的原因与解决方案咨询

.NET 8.0项目CS0579重复属性错误的问题解答

背景

在Ubuntu 22.04上开发.NET 8.0项目,系统自动更新至.NET SDK 8.0.409后,5月23日未修改代码却出现以下CS0579重复属性错误:

/home/username/projects/MySolution/MyProject/obj/Debug/net8.0/MyProject.AssemblyInfo.cs(13,12): error CS0579: Duplicate 'System.Reflection.AssemblyCompanyAttribute' attribute [/home/username/projects/MySolution/MyProject/MyProject.csproj]
/home/username/projects/MySolution/MyProject/obj/Debug/net8.0/MyProject.AssemblyInfo.cs(14,12): error CS0579: Duplicate 'System.Reflection.AssemblyConfigurationAttribute' attribute [/home/username/projects/MySolution/MyProject/MyProject.csproj]
/home/username/projects/MySolution/MyProject/obj/Debug/net8.0/MyProject.AssemblyInfo.cs(16,12): error CS0579: Duplicate 'System.Reflection.AssemblyFileVersionAttribute' attribute [/home/username/projects/MySolution/MyProject/MyProject.csproj]
/home/username/projects/MySolution/MyProject/obj/Debug/net8.0/MyProject.AssemblyInfo.cs(17,12): error CS0579: Duplicate 'System.Reflection.AssemblyInformationalVersionAttribute' attribute [/home/username/projects/MySolution/MyProject/MyProject.csproj]
/home/username/projects/MySolution/MyProject/obj/Debug/net8.0/MyProject.AssemblyInfo.cs(18,12): error CS0579: Duplicate 'System.Reflection.AssemblyProductAttribute' attribute [/home/username/projects/MySolution/MyProject/MyProject.csproj]
/home/username/projects/MySolution/MyProject/obj/Debug/net8.0/MyProject.AssemblyInfo.cs(19,12): error CS0579: Duplicate 'System.Reflection.AssemblyTitleAttribute' attribute [/home/username/projects/MySolution/MyProject/MyProject.csproj]
/home/username/projects/MySolution/MyProject/obj/Debug/net8.0/MyProject.AssemblyInfo.cs(20,12): error CS0579: Duplicate 'System.Reflection.AssemblyVersionAttribute' attribute [/home/username/projects/MySolution/MyProject/MyProject.csproj]

删除bin/obj文件夹无效,临时解决方案是在.csproj中添加:

<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>

1. 未修改代码为何突发此类错误?

核心原因是自动生成的AssemblyInfo.cs与项目中已存在的手动定义程序集属性重复。
项目中可能遗留了手动创建的Properties/AssemblyInfo.cs文件(或其他位置的同名文件),里面已经定义了AssemblyCompany、AssemblyVersion等属性。之前的.NET SDK版本在自动生成obj/Debug/net8.0/MyProject.AssemblyInfo.cs时,可能会跳过这些已手动定义的属性;但SDK更新后,生成逻辑发生变化,不再检测手动定义的属性,直接生成了全套属性,导致编译时出现重复定义错误。

另外,系统更新后构建缓存的状态变化,也可能让原本被忽略的冲突直接暴露出来,但本质还是属性重复的问题。

2. 是否与SDK更新相关?

是的,这大概率和.NET SDK 8.0.409的更新有关。
.NET SDK的服务更新(比如8.0.x系列的小版本升级)会调整MSBuild构建目标的细节,其中就包括自动生成AssemblyInfo的规则。旧版本的SDK可能默认不生成部分程序集属性,或者会检测项目中是否已有手动定义并跳过生成;而8.0.409版本修改了这一逻辑,强制生成全套属性,从而和项目中已有的手动定义产生冲突。

3. 有无不禁用程序集信息自动生成的更优修复方法?

有三种更优方案,无需全局禁用自动生成:

  • 方案一:删除手动的AssemblyInfo.cs文件
    现在.NET SDK会自动根据.csproj中的配置(比如Company、Version等属性)生成程序集信息,不需要手动维护AssemblyInfo.cs。直接删除项目中手动创建的该文件,即可解决冲突,同时保留自动生成的便利性。

  • 方案二:逐个禁用重复的自动生成属性
    如果你需要保留手动的AssemblyInfo.cs,可以在.csproj中仅禁用那些和手动定义重复的属性,而不是全局关闭GenerateAssemblyInfo。例如:

    <GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
    <GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
    <GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
    <GenerateAssemblyInformationalVersionAttribute>false</GenerateAssemblyInformationalVersionAttribute>
    <GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
    <GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
    <GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
    

    这样既保留了SDK自动生成其他属性的能力,又避免了重复冲突。

  • 方案三:检查并更新冲突的NuGet包
    部分第三方NuGet包可能会注入自定义的MSBuild目标,干扰AssemblyInfo的生成逻辑。检查项目引用的NuGet包,将其更新至兼容.NET 8.0.409的版本,可能解决冲突。


内容的提问来源于stack exchange,提问作者Po-Sen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 01:58:12