dreinoso
dreinoso

Reputation: 1689

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?

Upvotes: 8

Views: 4957

Answers (2)

CodeCaster
CodeCaster

Reputation: 151710

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.

Upvotes: 12

AlanT
AlanT

Reputation: 3663

To expand upon the comment from @CodeCaster, the attribute should be placed in

AssemblyInfo.cs

in the Properties section of your project

e.g.

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: InternalsVisibleTo("UnitTests")]

you will also need to fully qualify the assembly name

e.g.

[assembly: InternalsVisibleTo("Foo.Bar.UnitTests")]

Upvotes: 5

Related Questions