23

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?

Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
MVinca
  • 291
  • 2
  • 4
  • 11

3 Answers3

38

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>
Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
  • 5
    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. – MVinca Jun 12 '17 at 16:11
  • 11
    FYI the 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: https://github.com/https://github.com/Microsoft/msbuild/issues/2281 – jrr Nov 01 '17 at 18:52
  • Does this mechanism only work for .Net Core project types? You marked [my question](https://stackoverflow.com/questions/56835671/how-to-read-a-msbuild-property-in-a-given-project-in-runtime?noredirect=1#comment100232012_56835671) as a duplicate, pointing to this MSBuild driven solution, yet I was unable to make it work on my .Net 4.6.2 project. – julealgon Jul 02 '19 at 12:26
  • @julealgon sorry that question didn't contain project details. If you want the same behavior as in core, i suggest https://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) – Martin Ullrich Jul 02 '19 at 12:57
  • @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. – julealgon Jul 02 '19 at 13:00
3

With .NET 5.0, you can use AssemblyMetadata:

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

Vadim Peretokin
  • 2,221
  • 3
  • 29
  • 40
2

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.

frozzen10
  • 101
  • 4