(When applying
an attribute whose name ends with Attribute, the C# compiler will apply the
suffix automatically.)
InternalsVisibleTo can only be applied to an assembly (not a specific member),
and you can apply it multiple times to the same assembly. We will call the assembly
containing the attribute the source assembly, although this is entirely unofficial terminology.
When you apply the attribute, you have to specify another assembly, known as
the friend assembly. The result is that the friend assembly can see all the internal members
of the source assembly as if they were public. This may sound alarming, but it can
be useful, as we??™ll see in a minute.
Listing 7.12 shows this with three classes in three different assemblies.
3 Using reflection when running with suitable permissions doesn??™t count.
202 CHAPTER 7 Concluding C# 2: the final features
// Compiled to Source.dll
using System.Runtime.CompilerServices;
[assembly:InternalsVisibleTo("FriendAssembly")]
public class Source
{
internal static void InternalMethod()
{
}
public static void PublicMethod()
{
}
}
// Compiled to FriendAssembly.dll
public class Friend
{
static void Main()
{
Source.InternalMethod();
Source.PublicMethod();
}
}
// Compiled to EnemyAssembly.dll
public class Enemy
{
static void Main()
{
// Source.InternalMethod();
Source.PublicMethod();
}
}
In listing 7.
Pages:
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395