C# – Attributo "AssemblyVersion" duplicato

C# – Attributo "AssemblyVersion" duplicato

Problema

Stai cercando di aggiungere l'attributo AssemblyVersion al tuo progetto, in questo modo:

using System.Reflection;

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

Code language: C# (cs)

E ottieni i seguenti errori del compilatore:

Ma non vedi questi attributi da nessun'altra parte nel tuo progetto.

Soluzione

Il problema è che Visual Studio genera automaticamente le informazioni sull'assembly per impostazione predefinita.

Per disattivare questa opzione, inserisci quanto segue nel tuo file .csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>
  <PropertyGroup>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  </PropertyGroup>
</Project>

Code language: HTML, XML (xml)

Dove sono le informazioni sull'assieme generato automaticamente?

Il mio assembly si chiama DupeAssemblyVersion e mi rivolgo a .NET Core 3.1. Quindi il file di informazioni sull'assembly generato automaticamente è qui:\obj\Debug\netcoreapp3.1\DupeAssemblyVersion.AssemblyInfo.cs.

Ecco come appare questo file:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Reflection;

[assembly: System.Reflection.AssemblyCompanyAttribute("DupeAssemblyVersion")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyProductAttribute("DupeAssemblyVersion")]
[assembly: System.Reflection.AssemblyTitleAttribute("DupeAssemblyVersion")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

// Generated by the MSBuild WriteCodeFragment class.
Code language: C# (cs)