我有一个项目,在编译时产生以下错误:

重复'AssemblyVersion'属性

我检查了文件AssemblyInfo.cs,看起来没有重复。

我在MSDN上找到了这篇文章,它解决了一个类似的问题,并按照这篇文章中的建议解决了这个问题。

有人能告诉我这是怎么回事吗?这种情况是否只发生在有两个或多个类名称相似的项目的情况下?还是其他原因?


当前回答

obj\Debug\netstandard2.0\PacktLibrary.AssemblyInfo.cs(15,12): error CS0579: Duplicate 'System.Reflection.AssemblyConfigurationAttribute' attribute [c:\Users\John_Tosh1\Documents\C#8.0and.NetCore3.0\Code\Chapter05\PacktLibrary\PacktLibrary.csproj]
obj\Debug\netstandard2.0\PacktLibrary.AssemblyInfo.cs(16,12): error CS0579: Duplicate 'System.Reflection.AssemblyFileVersionAttribute' attribute [c:\Users\John_Tosh1\Documents\C#8.0and.NetCore3.0\Code\Chapter05\PacktLibrary\PacktLibrary.csproj]
obj\Debug\netstandard2.0\PacktLibrary.AssemblyInfo.cs(17,12): error CS0579: Duplicate 'System.Reflection.AssemblyInformationalVersionAttribute' attribute [c:\Users\John_Tosh1\Documents\C#8.0and.NetCore3.0\Code\Chapter05\PacktLibrary\PacktLibrary.csproj]
obj\Debug\netstandard2.0\PacktLibrary.AssemblyInfo.cs(18,12): error CS0579: Duplicate 'System.Reflection.AssemblyProductAttribute' attribute [c:\Users\John_Tosh1\Documents\C#8.0and.NetCore3.0\Code\Chapter05\PacktLibrary\PacktLibrary.csproj]
obj\Debug\netstandard2.0\PacktLibrary.AssemblyInfo.cs(19,12): error CS0579: Duplicate 'System.Reflection.AssemblyTitleAttribute' attribute [c:\Users\John_Tosh1\Documents\C#8.0and.NetCore3.0\Code\Chapter05\PacktLibrary\PacktLibrary.csproj]
obj\Debug\netstandard2.0\PacktLibrary.AssemblyInfo.cs(20,12): error CS0579: Duplicate 'System.Reflection.AssemblyVersionAttribute' attribute [c:\Users\John_Tosh1\Documents\C#8.0and.NetCore3.0\Code\Chapter05\PacktLibrary\PacktLibrary.csproj]

我相信我的库文件夹是由于无意中创建了另一个类库而损坏的。我删除了所有相关文件库,但问题仍然存在。我通过删除目录中的所有bin和obj文件夹找到了一个解决方案。之前的构建是正常的,但是发现了一个具有相同的assemblyinfo.cs文件的子文件夹。

其他回答

当我的主项目与解决方案位于同一文件夹中时,我就遇到了这个问题,然后我在同一解决方案中有一个位于子文件夹中的独立项目,并且该独立项目使用主项目作为参考。这导致主项目检测到子文件夹bin & obj文件夹创建重复的引用。

在将旧项目转换为. net Core时,现在可以在项目本身设置AssemblyInfo.cs中的大部分信息。打开项目属性并选择Package选项卡以查看新的设置。

埃里克·l·安德森的帖子 “复制”System.Reflection。AssemblyCompanyAttribute ' attribute"描述了3个选项:

从AssemblyInfo.cs文件中删除冲突项, 完全删除文件或 禁用GenerateAssemblyInfo(在Serge Semenov的另一个回答中建议)

创建项目时,Visual Studio会将其设置为编译并生成相应的程序集。每个项目生成一个程序集,因此每个项目都有相应的程序集配置来生成其程序集。

问题是,当您创建多个项目时,每个项目都可以生成自己的程序集,然后将其中一个项目包含在另一个项目中。

在这种情况下,Visual Studio会感到困惑,不知道从哪个配置文件开始为项目生成单个程序集——它会在包含的项目中找到第二个程序集配置,并说“HEY, DUPLICATE!你给了我两套指令来生成我的程序集!”

但有时您仍然希望所包含的项目能够自行生成程序集,但当它被包含在另一个项目中时,则不能这样做。

要获得这一点,一种解决方案是向包含项目的项目(在项目属性中可以找到)添加条件定义。然后更改所包含项目中的程序集配置,以查找此条件定义。如果它是定义的(由包括项目),那么配置可以跳过它的内容——这将导致只有一个配置被VS发现——从包括项目——问题解决了!

以下步骤(包括截图)

步骤1:选择包含/容器项目>右键单击>属性

项目属性(截图)

步骤2:导航到Build > General >条件编译符号

添加如下所示的条件定义:

条件编译符号(截图)

步骤3:在包含的项目AssemblyInfo.cs中使用条件定义

使用条件定义(截图)

如果我在Visual Studio 2017中编译项目,然后尝试用. net Core用命令行命令“dotnet run”重建并运行它,通常会发生这种情况。

简单地删除所有“bin”和“obj”文件夹——包括在“ClientApp”内部和直接在项目文件夹中——允许. net Core命令“dotnet run”重新构建并成功运行。

obj\Debug\netstandard2.0\PacktLibrary.AssemblyInfo.cs(15,12): error CS0579: Duplicate 'System.Reflection.AssemblyConfigurationAttribute' attribute [c:\Users\John_Tosh1\Documents\C#8.0and.NetCore3.0\Code\Chapter05\PacktLibrary\PacktLibrary.csproj]
obj\Debug\netstandard2.0\PacktLibrary.AssemblyInfo.cs(16,12): error CS0579: Duplicate 'System.Reflection.AssemblyFileVersionAttribute' attribute [c:\Users\John_Tosh1\Documents\C#8.0and.NetCore3.0\Code\Chapter05\PacktLibrary\PacktLibrary.csproj]
obj\Debug\netstandard2.0\PacktLibrary.AssemblyInfo.cs(17,12): error CS0579: Duplicate 'System.Reflection.AssemblyInformationalVersionAttribute' attribute [c:\Users\John_Tosh1\Documents\C#8.0and.NetCore3.0\Code\Chapter05\PacktLibrary\PacktLibrary.csproj]
obj\Debug\netstandard2.0\PacktLibrary.AssemblyInfo.cs(18,12): error CS0579: Duplicate 'System.Reflection.AssemblyProductAttribute' attribute [c:\Users\John_Tosh1\Documents\C#8.0and.NetCore3.0\Code\Chapter05\PacktLibrary\PacktLibrary.csproj]
obj\Debug\netstandard2.0\PacktLibrary.AssemblyInfo.cs(19,12): error CS0579: Duplicate 'System.Reflection.AssemblyTitleAttribute' attribute [c:\Users\John_Tosh1\Documents\C#8.0and.NetCore3.0\Code\Chapter05\PacktLibrary\PacktLibrary.csproj]
obj\Debug\netstandard2.0\PacktLibrary.AssemblyInfo.cs(20,12): error CS0579: Duplicate 'System.Reflection.AssemblyVersionAttribute' attribute [c:\Users\John_Tosh1\Documents\C#8.0and.NetCore3.0\Code\Chapter05\PacktLibrary\PacktLibrary.csproj]

我相信我的库文件夹是由于无意中创建了另一个类库而损坏的。我删除了所有相关文件库,但问题仍然存在。我通过删除目录中的所有bin和obj文件夹找到了一个解决方案。之前的构建是正常的,但是发现了一个具有相同的assemblyinfo.cs文件的子文件夹。