A Comprehensive Guide to Customizing Assembly Attributes in .NET Core

Nov 29, 2025 · Programming · 26 views · 7.8

Keywords: .NET Core | Assembly Attributes | csproj | Version Control | MSBuild

Abstract: This article provides an in-depth exploration of methods to customize assembly attributes in .NET Core projects. With the return to the .csproj format, the AssemblyInfo.cs file is now auto-generated, rendering traditional customization ineffective. It analyzes how to modify .csproj properties, disable auto-generation, or use Directory.Build.props for centralized management to address needs for customizing version, company info, and other assembly attributes. Code examples and step-by-step explanations aid developers in flexibly controlling assembly metadata.

Introduction

With .NET Core's return to the .csproj project format, the management of assembly attributes has undergone significant changes. The traditional AssemblyInfo.cs file is now auto-generated by the build system, stored in memory or the /obj/ directory, rather than in the project source code. This prevents developers from directly editing the file to define assembly attributes such as company name and version number. Based on common issues in practical development, this article systematically introduces multiple methods for customizing these attributes in the .NET Core environment.

Auto-Generated Assembly Attributes

In .NET Core projects, the SDK automatically generates a MyProject.AssemblyInfo.cs file containing assembly attributes, for example:

[assembly: AssemblyCompany("MyProject")]
[assembly: AssemblyVersion("1.0.0.0")]

These attributes are regenerated with each build, and developers cannot customize them by modifying this file. Attempting to redefine these attributes in a classic AssemblyInfo.cs results in compilation errors due to attribute conflicts.

Method 1: Defining Attributes in the .csproj File

The most straightforward approach is to set relevant properties in the project file (.csproj). For instance, to define the company name and version, add the following code:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <Company>My Company</Company>
    <Version>1.2.3.4</Version>
    <AssemblyVersion>2.0.0.0</AssemblyVersion>
    <FileVersion>3.0.0.0</FileVersion>
  </PropertyGroup>
</Project>

Here, the Company property sets the assembly company information, Version is used for the NuGet package version, while AssemblyVersion and FileVersion control the assembly version and file version, respectively. During build, these values are automatically injected into the generated assembly attributes.

Method 2: Disabling Auto-Generation of Assembly Attributes

If you prefer using the traditional AssemblyInfo.cs file, you can disable the auto-generation feature. Add the following to the .csproj:

<PropertyGroup>
  <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>

After this setting, the system no longer auto-generates the assembly attributes file. Developers can manually create AssemblyInfo.cs and directly define the required attributes, for example:

using System.Reflection;

[assembly: AssemblyCompany("Custom Company")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

This method is suitable for scenarios requiring full control over attribute definitions or when migrating old projects.

Method 3: Centralized Management with Directory.Build.props

For multi-project solutions, using a Directory.Build.props file is recommended for centralized property management. Create this file in the solution root directory and add shared properties:

<Project>
  <PropertyGroup>
    <Company>Shared Company</Company>
    <Copyright>Copyright © 2023</Copyright>
    <Version>1.0.0.1</Version>
    <AssemblyVersion>1.0.0.1</AssemblyVersion>
    <FileVersion>1.0.0.1</FileVersion>
  </PropertyGroup>
</Project>

MSBuild automatically applies these properties to all sub-projects, simplifying the maintenance of version and company information. Additionally, these settings affect NuGet packages generated via dotnet pack.

Underlying Mechanisms and Extensions

The auto-generation feature is controlled by the Microsoft.NET.GenerateAssemblyInfo.targets file, which is part of the .NET SDK. A deeper understanding of this mechanism aids in customizing the build process. For example, developers can modify the targets file or use custom MSBuild tasks to extend functionality.

Practical Application Recommendations

In development, choose the appropriate method based on project needs: use .csproj properties for single projects; disable auto-generation when traditional control is needed; prioritize Directory.Build.props for multi-project environments. Note that properties like Version impact package management, while AssemblyVersion relates to assembly compatibility and should be set carefully.

Conclusion

.NET Core's management of assembly attributes has been modernized through .csproj and the build system. The three methods outlined in this article cover common scenarios, helping developers efficiently customize metadata. Combining official documentation and community practices can further optimize the build process and enhance project maintainability.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.