Skip to content

Add information about how to add your own AssemblyAttribute values #37991

@baronfel

Description

@baronfel

Type of issue

Missing information

Description

I was asked about how to extend AssemblyInfo generation in the SDK to include arbitrary AssemblyAttributes. This is a thing that is doable relatively easily, but there are no examples. Other parts of the ecosystem like GitVersion, Source Link, and Ionide.KeepAChangelog use this successfully to embed AssemblyAttributes, but we had to dig into sources to figure out how it is done. A simple example here would at least be searchable by users.

I'm thinking a new subsection under the GenerateAssemblyInfo property description table that would talk about how to add your own custom attributes.

Example content

It's possible to add your own AssemblyAttributes to this generated file as well. To do so, you need to define AssemblyAttribute MSBuild Items that tell the SDK what Type of attribute to create, as well as any constructor parameters that are required for that attribute. For example, the AssemblyMetadataAttribute in the System.Reflection namespace has a constructor that takes two strings:

  • a name to describe an arbitrary value
  • a value to actually store

If you had a Date Property in MSBuild that contained the date string that an assembly was created on, you could use AssemblyMetadataAttribute to embed that date into the assembly attributes using the following MSBuild code:

<ItemGroup>
  <!- Include must be the fully-qualified .NET Type name of the Attribute to create -->
  <AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
    <!-- _Parameter1, _Parameter2, ..., _ParameterN  all correspond to the matching parameter of a constructor of that .NET Attribute Type -->
    <_Parameter1>BuildDate</_Parameter1>
    <_Parameter2>$(Date)</_Parameter2>
  </AssemblyAttribute>
</ItemGroup>

This would tell the SDK to emit the following C# (or equivalent F# or VB) as an assembly-level attribute:

[assembly: System.Reflection.AssemblyMetadataAttribute("BuildDate", "11/9/2023")]

The actual Date string would be whatever you provided at the time of the build.

Other remarks

It seems to me that the SDK reference docs might be a good place to have a 'recipes' section where we can go into a bit more detail about how to customize the SDK's behaviors for various topics. Embedding content like this^ into the long property list reference pages might be overwhelming, and there often isn't a perfect fit for a specific piece of content. Having a place for the SDK or MSBuild teams to author focused docs on specific patterns seems like a win from a search/reference perspective.

Page URL

https://learn.microsoft.com/en-us/dotnet/core/project-sdk/msbuild-props

Content source URL

https://github.com/dotnet/docs/blob/main/docs/core/project-sdk/msbuild-props.md

Document Version Independent Id

6741b1de-38a4-fbb1-d79d-acc47d549a2d

Article author

gewarren


Associated WorkItem - 189684

Metadata

Metadata

Assignees

Labels

📌 seQUESTeredIdentifies that an issue has been imported into Quest.🗺️ reQUESTTriggers an issue to be imported into Quest.dotnet-fundamentals/svcin-prThis issue will be closed (fixed) by an active pull request.

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions