Prev | Current Page 384 | Next

Jon Skeet

"C# in Depth: What you need to master C# 2 and 3"

12 a special relationship exists between FriendAssembly.dll and
Source.dll??”although it only operates one way: Source.dll has no access to internal
members of FriendAssembly.dll. If we were to uncomment the line at B, the Enemy
class would fail to compile.
So, why on earth would we want to open up our well-designed assembly to certain
assemblies to start with?
7.7.2 Why use InternalsVisibleTo?
I can??™t say I??™ve ever used InternalsVisibleTo between two production assemblies.
I??™m not saying there aren??™t legitimate use cases for that, but I??™ve not come across
them. However, I have used the attribute when it comes to unit testing.
There are some who say you should only test the public interface to code. Personally
I??™m happy to test whatever I can in the simplest manner possible. Friend assemblies
make that a lot easier: suddenly it??™s trivial to test code that only has internal access without
taking the dubious step of making members public just for the sake of testing, or
including the test code within the production assembly. (It does occasionally mean
Listing 7.12 Demonstration of friend assemblies
Grants
additional access
Uses additional
access within
FriendAssembly
EnemyAssembly has
no special access
B
Accesses public
method as normal
203 Exposing internal members to selected assemblies
making members internal for the sake of testing where they might otherwise be private,
but that??™s a less worrying step.


Pages:
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396