Why do we use InternalsVisibleToAtrribute even after giving the reference of one project to another in C#?

Viewed 27

I made 2 projects in which there are internal members and i want to call some internals from one function, say funcA, to another project. So I gave reference of the project to another. Why will i use the InteralsVisibleToAttribute if by giving the reference would make it visible in the project?

1 Answers

As indicated in the comment, the use of InternalsVisibleTo is a code smell (something that may make the code look poor). The only "correct" use of it is for unit testing, where you want to be able to test internal methods without making them public for all library users.

As to the "why", that's what it is for. InternalsVisibleTo is used to declare that any elements (methods, classes or fields) inside that library that are declared internal are to be seen as if they where public for the indicated library only. So a declaration such as

[assembly: InternalsVisibleTo("MyLibraryUnitTest, PublicKey=....")]

tells the compiler that any internal members of this library are to be made visible for MyLibraryUnitTest.

Related