Visual Studio 2017 and the new .csproj InternalsVisibleTo
Solution 1
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>")]
Solution 2
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>
Solution 3
As of .NET 5 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.
Luka
Updated on June 14, 2022Comments
-
Luka almost 2 years
Where do I put InternalsVisibleTo from AssemblyInfo in the new Visual Studio 2017 .csproj project file?
-
Daniel Gimenez about 6 yearsThis technique is very useful. If you have multiple projects you can use a
directory.build.props
file in the solution root and have it apply to all of your projects. -
julealgon over 5 yearsIs there any official documentation for this approach that you could link to?
-
Ivan Zaruba over 5 yearsNot sure this approach is documented since it is rather customization than common practice. But if you would like to know how this works, please refer to the
Microsoft.NET.GenerateAssemblyInfo.targets
file github.com/dotnet/sdk/blob/release/2.1/src/Tasks/… -
yurislav over 5 yearsThis should be the accepted answer, as the question states to add InternalsVisibleTo to .csproj file.
-
Cocowalla about 5 yearsThis might be obvious, but it might be worth nothing that this won't work if you have
GenerateAssemblyVersionAttribute
set tofalse
-
Andy Danger Gagne almost 5 yearsAlternative to
($MSBuildProjectName)
is$(AssemblyName)
in the event your project name is different than your assembly. -
dashesy over 4 yearsNo need to use
PublicKey
in this case? -
Ivan Zaruba over 4 years@dashesy, this approach changes nothing as if you have used an attribute. You will need a <_Parameter1>BlaBla, PublicKey=Bla</_Parameter1> for a signed assembly.
-
void.pointer about 2 yearsIf you do end up adding this to your
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 calledFoo
isFoo.Tests
. This condition is built with that in mind.