Tempuslight
Tempuslight

Reputation: 1223

Adding "InternalsVisibleTo" to my .NET 6 csproj file doesn't work, but adding it above my internal class does work. Why doesn't it work in csproj?

So I'm upgrading projects from .NET framework 4.7.2 to .NET 6 and trying to remove all AssemblyInfo.cs files. In those files there's also settings to expose project internals to their respective "UnitTest" projects so that we can test these functions, etc.

From .NET 5 and onward it seems you can add this setting to your csproj file. I've done it in my "aaa.Application" project like this:

<ItemGroup>
    <InternalsVisibleTo Include="aaa.Application.UnitTests" />
</ItemGroup>

But this doesn't seem to be working. In my UnitTest project, it still gives the error that it's inaccessible due to its protection level. I've double checked and neither of the projects are strongly signed, so I can't add a PublicKey to this setting.

However when I add it above the class in my "aaa.Application" project, like this:

[assembly: InternalsVisibleTo("aaa.Application.UnitTests")]

Then it does work! However this solution isn't ideal as there are more internals that are exposed and it's OK for us to expose the entire project to it's respective UnitTest-project.

The UnitTest assemblyname is defined as follows in it's csproj-file:

<PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>disable</ImplicitUsings>
    <Nullable>disable</Nullable>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    <AssemblyName>aaa.Application.UnitTests</AssemblyName>
    <RootNamespace>aaa.Application.UnitTests</RootNamespace>
    <IsTestProject>true</IsTestProject>
</PropertyGroup>

And it autocompletes like this when adding the attribute above my class.

Does anyone know why the csproj implementation doesn't seem to be working?

Upvotes: 5

Views: 1631

Answers (2)

Tempuslight
Tempuslight

Reputation: 1223

The answer to my question was the following:

Our projects had the following setting

<GenerateAssemblyInfo>false</GenerateAssemblyInfo>

Because global assemblyinfo is being copied from somewhere else into each project. However, this caused my property "InternalsVisibleTo" to not be generated in my "aaa.Application" project!

When setting this setting to true, like this:

<GenerateAssemblyInfo>true</GenerateAssemblyInfo> 

It auto-generates an AssemblyInfo.cs class and adds the "InternalsVisibleTo" property to it and my UnitTest project builds now :)

Upvotes: 5

Norcino
Norcino

Reputation: 6627

Please try using this as follows:

<ItemGroup>
    <AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
        <_Parameter1>My.NameSpace.Tests</_Parameter1>
    </AssemblyAttribute>
</ItemGroup>

If the assembly you are exposing is a signed assembly, then use:

<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
  <_Parameter1>My.NameSpace.Tests, PublicKey={YOUR_PUBLIC_KEY</_Parameter1>
</AssemblyAttribute>

Upvotes: -3

Related Questions