How to use a custom attribute on an assembly in .NET Core 1.1
Asked Answered
C

4

26

While I found this post regarding retrieving a custom attribute on an assembly, I am unsure on how to add a custom attribute to an assembly in .NET Core 1.1. In .NET Framework, I would have done something like:

[assembly: AdditionalLocation(@"..\ReadFromHere")]

But my netcore1.1 projects in Visual Studio do not have an AssemblyInfo.cs. Where would I declare a custom attribute for an assembly? Is there something I can put in the .csproj file?

Calices answered 12/6, 2017 at 14:29 Comment(0)
A
40

You can always create a new AssemblyInfo.cs file or any other .cs file to do the same.

However you can also use the new auto-generated assembly info mechanism. You can add this to your csproj file, replace the value replacing the Include attributes value with the type name of your custom attribute:

<ItemGroup>
  <AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
    <_Parameter1>DasMulli.Win32.ServiceUtils.Tests</_Parameter1>
  </AssemblyAttribute>
</ItemGroup>
Ambiguous answered 12/6, 2017 at 14:38 Comment(6)
Never really thought about how there was nothing special about "AssemblyInfo.cs" but now that you mention it, it makes perfect sense that I could put an [assembly:...] directive anywhere. For that reason though, the .csproj just feels more right to me as a location for something assembly-wide. Thank you for that syntax, it was exactly what I was missing.Calices
FYI the <AssemblyAttribute> strategy only works for string values, so you can't use it to replace a non-string attribute that used to live in AssemblyInfo.cs (e.g. xunit's [assembly: Xunit.CollectionBehavior(Xunit.CollectionBehavior.CollectionPerAssembly)] ) Support for non-strings isn't likely to happen: github.com/https://github.com/Microsoft/msbuild/issues/2281Ticklish
Does this mechanism only work for .Net Core project types? You marked my question as a duplicate, pointing to this MSBuild driven solution, yet I was unable to make it work on my .Net 4.6.2 project.Xenophanes
@Xenophanes sorry that question didn't contain project details. If you want the same behavior as in core, i suggest github.com/dasMulli/AssemblyInfoGenerationSdk (you may need to set a few properties to disable some attribute generation if you want to keep your assemblyinfo.cs)Ambiguous
@MartinUllrich I managed to make it work. Please check my comments either in your post, or in my original question. I'd suggest not marking as duplicate though, since your answer does not provide a solution for .Net Framework projects.Xenophanes
You can find the documentation related to this answer here learn.microsoft.com/en-us/dotnet/standard/assembly/…Coruscate
P
6

I just come with this problem when moved my library from old .net framework to .net standard and I managed to solve the problem with adding non-string attribute that used to live in AssemblyInfo.cs. Here is fragment of my .csproj file and when run build my attribute was there:

<ItemGroup>
  <AssemblyAttribute Include="Xunit.CollectionBehavior">
    <_Parameter1>Xunit.CollectionBehavior.CollectionPerAssembly</_Parameter1>
    <_Parameter1_IsLiteral>true</_Parameter1_IsLiteral>  
    <_Parameter1_TypeName>Xunit.CollectionBehavior.CollectionPerAssembly</_Parameter1_TypeName>
  </AssemblyAttribute>
</ItemGroup>

It was added to msbuild as described here: https://github.com/dotnet/msbuild/issues/2281 with https://github.com/dotnet/msbuild/blob/main/documentation/Changelog.md#msbuild-16100 this release.

Hope it helps! Now this is way better with this.

Prem answered 18/1, 2023 at 14:7 Comment(0)
P
5

With .NET 5.0, you can use AssemblyMetadata:

<AssemblyMetadata Include="Bar" Value="Baz" />

Pace answered 12/5, 2021 at 18:11 Comment(1)
as an example for OP's problem: <ItemGroup><InternalsVisibleTo Include="Microsoft.Extensions.DependencyModel.Tests" /></ItemGroup>Microscopy
O
2

To further expand on the work of @Vadim. In .Net 5.0 you can use the AssemblyMetadata tag.

Example Below. This is for a .Net 7.0 Blazor project to put the build date, Git Hash, and Git, branch in the header of the webpage.

Project.csproj

<PropertyGroup>
    <!--Determines if compiler should add the attributes to the assembly-->
    <GenerateGitMetadata>True</GenerateGitMetadata>
<PropertyGroup>

<Target Name="AddGitMetadaAssemblyAttributes"
            BeforeTargets="GetAssemblyAttributes"
            Condition=" '$(GenerateGitMetadata)' == 'true' ">

    <!--Executes the Git Commands to get the Hash and Branch-->
    <Exec Command="git rev-parse --short=8  HEAD" ConsoleToMSBuild="true" StandardOutputImportance="low" IgnoreExitCode="true" Condition=" '$(CommitHash)' == '' ">
        <Output TaskParameter="ConsoleOutput" PropertyName="CommitHash" />
    </Exec>
    <Exec Command="git rev-parse --abbrev-ref HEAD" ConsoleToMSBuild="true" StandardOutputImportance="low" IgnoreExitCode="true" Condition=" '$(CommitBranch)' == '' ">
        <Output TaskParameter="ConsoleOutput" PropertyName="CommitBranch" />
    </Exec>

    <!--Generates the ItemGroup and all AssemblyMetadata Tags-->
    <ItemGroup>
        <AssemblyMetadata Include="web_BuildTimestamp" Value="$([System.DateTime]::UtcNow.ToString(yyyy-MM-ddTHH:mm:ssK))" />
        <AssemblyMetadata Condition=" $(CommitHash) != '' " Include="web_CommitHash" Value="$(CommitHash)" />
        <AssemblyMetadata Condition=" $(CommitBranch) != '' " Include="web_CommitBranch" Value="$(CommitBranch)" />
    </ItemGroup>
</Target>

Any class can now access the attributes with the following code.

var assemblyAttributes = Assembly.GetExecutingAssembly()
                        .GetCustomAttributes<AssemblyMetadataAttribute>()

For Example _Host.cshtml (at the top)

@{
string assemblyAttributePrefix = "web_";
var assemblyAttributes = Assembly.GetExecutingAssembly().GetCustomAttributes<AssemblyMetadataAttribute>().Where(x => x.Key.StartsWith(assemblyAttributePrefix));
}

In the Body

@foreach (var attr in assemblyAttributes)
{
    <meta name="@attr.Key.Replace(assemblyAttributePrefix,string.Empty)" content="@attr.Value" />
}
Offspring answered 8/11, 2023 at 20:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.