Introduction to Youyuan Assembly We know that members of a class that are defined as internal (including types, methods, properties, variables, events) can only be accessed in the same assembly (of course, I'm talking about the normal way here, not including access via reflection). This rule is in . .NET 2.0 is slightly broken, allowing us to set an assembly's internal members to be accessed by specific members. We call the assembly that contains these internal members the source assembly, and the assembly set is called the friend assembly.
Friend assembly use This Attribute is used at the assembly level.
This way, after the FriendAssembly program references the assembly, it can access the internal Name property. Generally speaking, this assembly:InternalsVisibleTo("FriendAssembly") is better placed in the AssemblyInfo.cs, after all, it is at the assembly level.
When to use InternalsToVisibleTo In fact, this attribute is not used in general applications, and we should not abuse this attribute. Because in general, a well-designed assembly does not need to be open to the outside world. But unit testing scenarios can take advantage of this feature. With this feature, we can specify that the code for unit tests has access to those internal members so that we can unit test those members. However, this requires writing the name of the friend assembly dead in the source assembly, which causes some security issues. Security can be guaranteed by introducing signature assemblies.
InternalsToVisibleTo and the signature assembly You can directly specify the name and public key of the friend meta assembly to InternalsVisibleTo. Use the VS developer command line to generate the public key for FriendAssembly:
Specify the name of the friend assembly and the public key to the source assembly:
In fact, since the friend assembly references the source assembly, and the signed assembly references an unsigned assembly, and the signed assembly cannot specify an unsigned friend assembly, any of them are signed, then they need to be signed.
|