Where do I put InternalsVisibleTo from AssemblyInfo in the new Visual Studio 2017 .csproj project file?
To clarify Hans Passant's comment above, you simply have to add InternalsVisibleTo to any cs file in your project. For example, I created an AssemblyInfo.cs file in the root of the project and then added the following content (only):
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=<ADD_KEY_HERE>")]
Just in case anyone would like to put InternalsVisibleTo
within a .csproj
file instead of AssemblyInfo.cs
(a possible scenario is to have a naming convention between a project under test and a test project), you can do it like this:
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>$(MSBuildProjectName).Test</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
Having this the following code will be generated
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("MyProject.Test")]
inside auto-generated AssemblyInfo.cs (e.g. for Debug configuration and .NET Standard 2.0 target)
/obj/Debug/netstandard2.0/MyProject.AssemblyInfo.cs
Additional Info
In case you're on .NET Core 3.1 and this approach isn't working, you may have to explicitly generate assembly info by adding the following to your .csproj
file:
<PropertyGroup>
<!-- Explicitly generate Assembly Info -->
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
</PropertyGroup>
directory.build.props
file in the solution root and have it apply to all of your projects. –
Dagnydago Microsoft.NET.GenerateAssemblyInfo.targets
file github.com/dotnet/sdk/blob/release/2.1/src/Tasks/… –
Infrastructure GenerateAssemblyVersionAttribute
set to false
–
Unschooled ($MSBuildProjectName)
is $(AssemblyName)
in the event your project name is different than your assembly. –
Naraka PublicKey
in this case? –
Greatgrandaunt Directory.Build.props
files, you want to make sure it's excluded from test projects. The following condition on my ItemGroup worked for me: <ItemGroup Condition="!$(ProjectName.EndsWith('.Tests'))">
. My test naming convention for a project called Foo
is Foo.Tests
. This condition is built with that in mind. –
Solingen As of .NET 5 (and newer .NET versions) this actually works once added to your csproj
:
<ItemGroup>
<InternalsVisibleTo Include="YourProject.Tests.Unit" />
</ItemGroup>
Work and discussion around this feature can be seen on this PR on dotnet's GitHub repo.
To clarify Hans Passant's comment above, you simply have to add InternalsVisibleTo to any cs file in your project. For example, I created an AssemblyInfo.cs file in the root of the project and then added the following content (only):
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=<ADD_KEY_HERE>")]
© 2022 - 2024 — McMap. All rights reserved.