Denr2 Denr2 - 12 days ago 5
C# Question

Is it possible to make all internal classes visible to another assembly in C# with some configuration?

I know that is possible change the visibility with:

using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("UnitTests")]

namespace Foobar
{
internal class Foo
{
//...
}
}


So my test project can create the test for the class
Foo
. The problem is that I have a lot of internal classes and I don't want to dirty them with that
assembly
expression. Is there another way of doing this
InternalsVisibleTo("UnitTests")
for the whole project?

Answer

The [InternalsVisibleTo] attribute is an assembly-level attribute, meaning that if you define it once, it already applies to the entire assembly.

So that line you show only needs to be included only once in your project to apply to all internal types.

I'd recommend moving it to where the other assembly-level attributes are specified: in the AssemblyInfo.cs file in the Properties folder of the project.

Comments